@Theory
  public void create_computeVerify(@FromDataPoints("templates") String templateName)
      throws Exception {
    KeysetHandle handle = KeysetHandle.generateNew(KeyTemplates.get(templateName));
    Mac mac = handle.getPrimitive(Mac.class);

    byte[] data = "data".getBytes(UTF_8);
    byte[] tag = mac.computeMac(data);
    mac.verifyMac(tag, data);

    KeysetHandle otherHandle = KeysetHandle.generateNew(KeyTemplates.get(templateName));
    Mac otherMac = otherHandle.getPrimitive(Mac.class);
    assertThrows(GeneralSecurityException.class, () -> otherMac.verifyMac(tag, data));

    byte[] invalid = "invalid".getBytes(UTF_8);
    byte[] empty = "".getBytes(UTF_8);
    assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(invalid, data));
    assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(tag, invalid));
    assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(empty, data));
    assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(tag, empty));
    mac.verifyMac(mac.computeMac(empty), empty);
  }
Unit tests for the Mac package. Uses only the public API.
java_src/src/test/java/com/google/crypto/tink/mac/MacTest.java