@Test
  public void monitors_associatesWithCorrectKey_works() throws Exception {
    FakeMonitoringClient fakeMonitoringClient = new FakeMonitoringClient();
    MutableMonitoringRegistry.globalInstance().clear();
    MutableMonitoringRegistry.globalInstance().registerMonitoringClient(fakeMonitoringClient);
    PublicKeySignWrapper.register();

    MonitoringAnnotations annotations =
        MonitoringAnnotations.newBuilder().add("annotation_name", "annotation_value").build();
    EcdsaParameters parameters =
        EcdsaParameters.builder()
            .setSignatureEncoding(EcdsaParameters.SignatureEncoding.IEEE_P1363)
            .setCurveType(EcdsaParameters.CurveType.NIST_P256)
            .setHashType(EcdsaParameters.HashType.SHA256)
            .setVariant(EcdsaParameters.Variant.TINK)
            .build();
    EcdsaParameters parametersNoPrefix =
        EcdsaParameters.builder()
            .setSignatureEncoding(EcdsaParameters.SignatureEncoding.IEEE_P1363)
            .setCurveType(EcdsaParameters.CurveType.NIST_P256)
            .setHashType(EcdsaParameters.HashType.SHA256)
            .setVariant(EcdsaParameters.Variant.NO_PREFIX)
            .build();

    KeysetHandle privateHandle =
        KeysetHandle.newBuilder()
            .addEntry(KeysetHandle.generateEntryFromParameters(parameters).withFixedId(20))
            .addEntry(
                KeysetHandle.generateEntryFromParameters(parametersNoPrefix)
                    .withFixedId(30)
                    .makePrimary())
            .addEntry(KeysetHandle.generateEntryFromParameters(parametersNoPrefix).withFixedId(40))
            .build();
    KeysetHandle publicHandle =
        KeysetHandle.newBuilder(privateHandle.getPublicKeysetHandle())
            .setMonitoringAnnotations(annotations)
            .build();

    PublicKeySign signer20 = EcdsaSignJce.create((EcdsaPrivateKey) privateHandle.getAt(0).getKey());
    PublicKeySign signer30 = EcdsaSignJce.create((EcdsaPrivateKey) privateHandle.getAt(1).getKey());
    PublicKeySign signer40 = EcdsaSignJce.create((EcdsaPrivateKey) privateHandle.getAt(2).getKey());
    PublicKeyVerify verifier = publicHandle.getPrimitive(PublicKeyVerify.class);

    // Verify once with data of length 2 for key with id 20.
    byte[] data = "da".getBytes(UTF_8);
    byte[] sig = signer20.sign(data);
    verifier.verify(sig, data);

    // Verify once with data of length 3 for key with id 30.
    data = "dat".getBytes(UTF_8);
    sig = signer30.sign(data);
    verifier.verify(sig, data);

    // Verify once with data of length 4 for key with id 40.
    data = "data".getBytes(UTF_8);
    sig = signer40.sign(data);
    verifier.verify(sig, data);

    List<FakeMonitoringClient.LogEntry> logEntries = fakeMonitoringClient.getLogEntries();
    assertThat(logEntries).hasSize(3);
    FakeMonitoringClient.LogEntry verify0Entry = logEntries.get(0);
    assertThat(verify0Entry.getKeyId()).isEqualTo(20);
    assertThat(verify0Entry.getPrimitive()).isEqualTo("public_key_verify");
    assertThat(verify0Entry.getApi()).isEqualTo("verify");
    assertThat(verify0Entry.getNumBytesAsInput()).isEqualTo(2);
    assertThat(verify0Entry.getKeysetInfo().getAnnotations()).isEqualTo(annotations);

    FakeMonitoringClient.LogEntry verify1Entry = logEntries.get(1);
    assertThat(verify1Entry.getKeyId()).isEqualTo(30);
    assertThat(verify1Entry.getPrimitive()).isEqualTo("public_key_verify");
    assertThat(verify1Entry.getApi()).isEqualTo("verify");
    assertThat(verify1Entry.getNumBytesAsInput()).isEqualTo(3);
    assertThat(verify1Entry.getKeysetInfo().getAnnotations()).isEqualTo(annotations);

    FakeMonitoringClient.LogEntry verify2Entry = logEntries.get(2);
    assertThat(verify2Entry.getKeyId()).isEqualTo(40);
    assertThat(verify2Entry.getPrimitive()).isEqualTo("public_key_verify");
    assertThat(verify2Entry.getApi()).isEqualTo("verify");
    assertThat(verify2Entry.getNumBytesAsInput()).isEqualTo(4);
    assertThat(verify2Entry.getKeysetInfo().getAnnotations()).isEqualTo(annotations);

    List<FakeMonitoringClient.LogFailureEntry> failures =
        fakeMonitoringClient.getLogFailureEntries();
    assertThat(failures).hasSize(0);
  }
We test all variants for legacy reasons.
java_src/src/test/java/com/google/crypto/tink/signature/PublicKeyVerifyWrapperTest.java