@Test
public void testGetAead() throws Exception {

  byte[] plaintext = "some plaintext string".getBytes(UTF_8);
  byte[] associatedData = "some associated data".getBytes(UTF_8);
  int count = 0;
  for (KeyTemplate template : keyTemplates) {
    RegistryEciesAeadHkdfDemHelper helper = new RegistryEciesAeadHkdfDemHelper(template);
    byte[] symmetricKey = Random.randBytes(helper.getSymmetricKeySizeInBytes());
    Aead aead = helper.getAead(symmetricKey);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    byte[] decrypted = aead.decrypt(ciphertext, associatedData);
    assertArrayEquals(plaintext, decrypted);
    // Try using a symmetric key that is too short.
    symmetricKey = Random.randBytes(helper.getSymmetricKeySizeInBytes() - 1);
    try {
      aead = helper.getAead(symmetricKey);
      fail("Symmetric key too short, should have thrown exception:\n" + template.toString());
    } catch (GeneralSecurityException e) {
      // Expected.
      assertExceptionContains(e, "incorrect length");
    }
    // Try using a symmetric key that is too long.
    symmetricKey = Random.randBytes(helper.getSymmetricKeySizeInBytes() + 1);
    try {
      aead = helper.getAead(symmetricKey);
      fail("Symmetric key too long, should have thrown exception:\n" + template.toString());
    } catch (GeneralSecurityException e) {
      // Expected.
      assertExceptionContains(e, "incorrect length");
    }
    count++;
  }
  assertEquals(keyTemplates.length, count);
}