KeysetHandle { public void write(KeysetWriter keysetWriter, Aead masterKey) throws GeneralSecurityException, IOException { EncryptedKeyset encryptedKeyset = encrypt(keyset, masterKey); keysetWriter.write(encryptedKeyset); return; } private  KeysetHandle(Keyset keyset); KeysetInfo getKeysetInfo(); @Deprecated static final KeysetHandle generateNew(com.google.crypto.tink.proto.KeyTemplate keyTemplate); static final KeysetHandle generateNew(KeyTemplate keyTemplate); static final KeysetHandle read(KeysetReader reader, Aead masterKey); static final KeysetHandle readNoSecret(KeysetReader reader); static final KeysetHandle readNoSecret(final byte[] serialized); void write(KeysetWriter keysetWriter, Aead masterKey); void writeNoSecret(KeysetWriter writer); KeysetHandle getPublicKeysetHandle(); @Override String toString(); static void assertEnoughKeyMaterial(Keyset keyset); static void assertEnoughEncryptedKeyMaterial(EncryptedKeyset keyset); P getPrimitive(Class<P> targetClassObject); P getPrimitive(KeyManager<P> customKeyManager, Class<P> targetClassObject);  }
@Test public void testEncryptFailed() throws Exception { KeysetHandle handle = KeysetManager.withEmptyKeyset() .rotate(MacKeyTemplates.HMAC_SHA256_128BITTAG) .getKeysetHandle(); TestUtil.DummyAead faultyAead = new TestUtil.DummyAead(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); KeysetWriter writer = BinaryKeysetWriter.withOutputStream(outputStream); try { handle.write(writer, faultyAead); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "dummy"); } }