@Test
public void encrypt_decrypt_success() throws GeneralSecurityException {

  if (TestUtil.isTsan()) {
    // RsaKem.generateRsaKeyPair is too slow in Tsan.
    return;
  }
  KeyPair keyPair = RsaKem.generateRsaKeyPair(2048);
  String hmacAlgo = "HMACSHA256";
  byte[] salt = Random.randBytes(20);
  RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
  HybridEncrypt hybridEncrypt =
      new RsaKemHybridEncrypt(rsaPublicKey, hmacAlgo, salt, new AesGcmFactory(16));
  RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
  HybridDecrypt hybridDecrypt =
      new RsaKemHybridDecrypt(rsaPrivateKey, hmacAlgo, salt, new AesGcmFactory(16));
  byte[] plaintext = Random.randBytes(20);
  byte[] context = Random.randBytes(20);
  // Makes sure that the encryption is randomized.
  Set<String> ciphertexts = new TreeSet<>();
  for (int j = 0; j < 8; j++) {
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, context);
    if (ciphertexts.contains(new String(ciphertext, UTF_8))) {
      throw new GeneralSecurityException("Encryption is not randomized");
    }
    ciphertexts.add(new String(ciphertext, UTF_8));
    byte[] decrypted = hybridDecrypt.decrypt(ciphertext, context);
    assertArrayEquals(plaintext, decrypted);
  }
  assertThat(ciphertexts).hasSize(8);
}