@Test
public void testEncryptDecryptWithVaryingRecordSizes() throws Exception {

  KeyPair uaKeyPair = EllipticCurves.generateKeyPair(WebPushConstants.NIST_P256_CURVE_TYPE);
  ECPrivateKey uaPrivateKey = (ECPrivateKey) uaKeyPair.getPrivate();
  ECPublicKey uaPublicKey = (ECPublicKey) uaKeyPair.getPublic();
  byte[] authSecret = Random.randBytes(16);
  int numTests = 100;
  if (TestUtil.isTsan()) {
    numTests = 5;
  }
  // Test with random, valid record sizes.
  for (int i = 0; i < numTests; i++) {
    int recordSize =
        WebPushConstants.CIPHERTEXT_OVERHEAD
            + Random.randInt(
                WebPushConstants.MAX_CIPHERTEXT_SIZE - WebPushConstants.CIPHERTEXT_OVERHEAD);
    HybridEncrypt hybridEncrypt =
        new WebPushHybridEncrypt.Builder()
            .withRecordSize(recordSize)
            .withAuthSecret(authSecret)
            .withRecipientPublicKey(uaPublicKey)
            .build();
    HybridDecrypt hybridDecrypt =
        new WebPushHybridDecrypt.Builder()
            .withRecordSize(recordSize)
            .withAuthSecret(authSecret)
            .withRecipientPublicKey(uaPublicKey)
            .withRecipientPrivateKey(uaPrivateKey)
            .build();
    byte[] plaintext = Random.randBytes(recordSize - WebPushConstants.CIPHERTEXT_OVERHEAD);
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, null /* contextInfo */);
    assertEquals(ciphertext.length, plaintext.length + WebPushConstants.CIPHERTEXT_OVERHEAD);
    assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, null /* contextInfo */));
  }
}