@Test
  public void getPublicKeysetHandle_keysetWithInvalidKey() throws Exception {
    // JwtSignatureConfig is not yet registered.

    // Because there is no parser for JwtEcdsaPrivateKey, the entries of privateHandle
    // contain one key of type LegacyProtoKey.
    KeysetHandle privateHandle =
        TinkJsonProtoKeysetFormat.parseKeyset(
            KEYSET_WITH_INVALID_JWT_KEY, InsecureSecretKeyAccess.get());
    Key key = privateHandle.getAt(0).getKey();
    assertThat(key).isInstanceOf(LegacyProtoKey.class);
    // getPublicKeysetHandle fails, because it requires a registered key manager.
    assertThrows(GeneralSecurityException.class, privateHandle::getPublicKeysetHandle);

    // JwtSignatureConfig registers parsers for JwtEcdsaPrivateKey and JwtEcdsaPublicKey,
    // and a key manager for JwtEcdsaPrivateKey that can implements getPublicKeyData
    JwtSignatureConfig.register();

    // getPublicKeysetHandle now works, because the key manager is now registered.
    KeysetHandle publicHandle = privateHandle.getPublicKeysetHandle();
    // But the public key can't be parsed, so getAt fails.
    assertThrows(IllegalStateException.class, () -> publicHandle.getAt(0));

    // parseKeyset now uses the parser for JwtEcdsaPrivateKey, but parsing fails.
    KeysetHandle privateHandle2 =
        TinkJsonProtoKeysetFormat.parseKeyset(
            KEYSET_WITH_INVALID_JWT_KEY, InsecureSecretKeyAccess.get());
    assertThrows(IllegalStateException.class, () -> privateHandle2.getAt(0));
    // getPublicKeysetHandle still work, because it uses the unparsed proto key.
    KeysetHandle publicHandle2 = privateHandle2.getPublicKeysetHandle();
    // But also parsing of the public key fails.
    assertThrows(IllegalStateException.class, () -> publicHandle2.getAt(0));
    // serializeKeysetWithoutSecret works, because it uses the unparsed proto keyset.
    String publicJsonKeyset2 = TinkJsonProtoKeysetFormat.serializeKeysetWithoutSecret(publicHandle);
    assertThat(publicJsonKeyset2).contains("JwtEcdsaPublicKey");
  }
Tests that key parsing via the serialization registry works as expected.

<p>NOTE: This adds a parser to the MutableSerializationRegistry, which no other test uses.
java_src/src/test/java/com/google/crypto/tink/KeysetHandleTest.java