@Test
  public void testEncryptDecryptNettyCompositeByteBuf() throws Exception {
    // testEncryptDecryptNettyByte already tests the correctness of the encrypt decrypt methods with non-composite Netty
    // ByteBuf, in this test case, we can make the assumption that these two functions always provide correct answers.

    String key = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
    Properties props = getKMSProperties(key, DEFAULT_KEY_SIZE_IN_CHARS);
    VerifiableProperties verifiableProperties = new VerifiableProperties((props));
    SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decode(key), "AES");
    GCMCryptoService cryptoService =
        (GCMCryptoService) (new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService());
    byte[] fixedIv = new byte[12];
    for (int i = 0; i < 5; i++) {
      int size = TestUtils.RANDOM.nextInt(MAX_DATA_SIZE - MIN_DATA_SIZE) + MIN_DATA_SIZE;
      byte[] randomData = new byte[size];
      TestUtils.RANDOM.nextBytes(randomData);

      ByteBuf toEncrypt = PooledByteBufAllocator.DEFAULT.ioBuffer(randomData.length);
      toEncrypt.writeBytes(randomData);
      CompositeByteBuf toEncryptComposite = toEncrypt.alloc().compositeBuffer(size);
      int start = 0;
      int end = 0;
      for (int j = 0; j < 3; j++) {
        start = end;
        end = TestUtils.RANDOM.nextInt(size / 2 - 1) + end;
        if (j == 2) {
          end = size;
        }
        ByteBuf c = PooledByteBufAllocator.DEFAULT.ioBuffer(end - start);
        c.writeBytes(randomData, start, end - start);
        toEncryptComposite.addComponent(true, c);
      }

      ByteBuf encryptedBytes = cryptoService.encrypt(toEncrypt, secretKeySpec, fixedIv);
      ByteBuf encryptedBytesComposite = cryptoService.encrypt(toEncryptComposite, secretKeySpec, fixedIv);
      Assert.assertEquals(encryptedBytes.readableBytes(), encryptedBytesComposite.readableBytes());
      Assert.assertEquals(toEncrypt.readableBytes(), 0);
      Assert.assertEquals(toEncryptComposite.readableBytes(), 0);

      byte[] array = new byte[encryptedBytes.readableBytes()];
      encryptedBytes.getBytes(encryptedBytes.readerIndex(), array);
      byte[] arrayComposite = new byte[encryptedBytesComposite.readableBytes()];
      encryptedBytesComposite.getBytes(encryptedBytesComposite.readerIndex(), arrayComposite);
      Assert.assertArrayEquals(array, arrayComposite);

      toEncrypt.release();
      toEncryptComposite.release();
      encryptedBytes.release();
      encryptedBytesComposite.release();

      ByteBuf toDecrypt = PooledByteBufAllocator.DEFAULT.ioBuffer(array.length);
      toDecrypt.writeBytes(array);
      CompositeByteBuf toDecryptComposite = toDecrypt.alloc().compositeBuffer(size);
      size = array.length;
      start = 0;
      end = 0;
      for (int j = 0; j < 3; j++) {
        start = end;
        end = TestUtils.RANDOM.nextInt(size / 2 - 1) + end;
        if (j == 2) {
          end = size;
        }
        ByteBuf c = PooledByteBufAllocator.DEFAULT.ioBuffer(end - start);
        c.writeBytes(array, start, end - start);
        toDecryptComposite.addComponent(true, c);
      }

      ByteBuf decryptedBytes = cryptoService.decrypt(toDecrypt, secretKeySpec);
      ByteBuf decryptedBytesComposite = cryptoService.decrypt(toDecryptComposite, secretKeySpec);

      Assert.assertEquals(decryptedBytes.readableBytes(), decryptedBytesComposite.readableBytes());
      Assert.assertEquals(toDecrypt.readableBytes(), 0);
      Assert.assertEquals(toDecryptComposite.readableBytes(), 0);

      array = new byte[decryptedBytes.readableBytes()];
      arrayComposite = new byte[decryptedBytesComposite.readableBytes()];
      decryptedBytes.getBytes(decryptedBytes.readerIndex(), array);
      decryptedBytesComposite.getBytes(decryptedBytesComposite.readerIndex(), arrayComposite);
      Assert.assertArrayEquals(array, arrayComposite);

      toDecrypt.release();
      toDecryptComposite.release();
      decryptedBytes.release();
      decryptedBytesComposite.release();
    }
  }
Test basic encryption and decryption for random data in {@link ByteBuf}.
@throws Exception Any unexpected error
ambry-router/src/test/java/com/github/ambry/router/GCMCryptoServiceTest.java