@Test
public void testRead() throws Exception {

  // Create a keyset that contains a single HmacKey.
  KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
  KeysetManager manager = KeysetManager.withEmptyKeyset().rotate(template);
  Keyset keyset1 = manager.getKeysetHandle().getKeyset();
  KeysetHandle handle1 =
      CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(keyset1.toByteArray()));
  assertEquals(keyset1, handle1.getKeyset());
  KeysetHandle handle2 = KeysetHandle.generateNew(template);
  Keyset keyset2 = handle2.getKeyset();
  assertEquals(1, keyset2.getKeyCount());
  Keyset.Key key2 = keyset2.getKey(0);
  assertEquals(keyset2.getPrimaryKeyId(), key2.getKeyId());
  assertEquals(template.getTypeUrl(), key2.getKeyData().getTypeUrl());
  Mac unused = handle2.getPrimitive(Mac.class);
}