@Test
public void testWrite() throws Exception {

  // Create a keyset that contains a single HmacKey.
  KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
  KeysetHandle handle = KeysetManager.withEmptyKeyset().rotate(template).getKeysetHandle();
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  KeysetWriter writer = BinaryKeysetWriter.withOutputStream(outputStream);
  CleartextKeysetHandle.write(handle, writer);
  ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
  KeysetReader reader = BinaryKeysetReader.withInputStream(inputStream);
  KeysetHandle handle2 = CleartextKeysetHandle.read(reader);
  assertEquals(handle.getKeyset(), handle2.getKeyset());
}