@Test
public void testInvalidKeyMaterial() throws Exception {

  byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE);
  byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE);
  int ivSize = 12;
  int tagSize = 16;
  Key valid =
      TestUtil.createKey(
          TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize),
          42,
          KeyStatusType.ENABLED,
          OutputPrefixType.RAW);
  Key invalid =
      TestUtil.createKey(
          TestUtil.createAesSivKeyData(64), 43, KeyStatusType.ENABLED, OutputPrefixType.TINK);
  KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(valid, invalid));
  try {
    keysetHandle.getPrimitive(Aead.class);
    fail("Expected GeneralSecurityException");
  } catch (GeneralSecurityException e) {
    assertExceptionContains(e, "com.google.crypto.tink.DeterministicAead");
  }
  // invalid as the primary key.
  keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(invalid, valid));
  try {
    keysetHandle.getPrimitive(Aead.class);
    fail("Expected GeneralSecurityException");
  } catch (GeneralSecurityException e) {
    assertExceptionContains(e, "com.google.crypto.tink.DeterministicAead");
  }
}