private static void checkCipher_DoFinal_invalidMode_Failure(int opmode) throws Exception {
        String msg = String.format(Locale.US,
                "doFinal() should throw IllegalStateException [mode=%d]", opmode);
        int bs = createAesCipher(opmode).getBlockSize();
        assertEquals(16, bs); // check test is set up correctly
        try {
            createAesCipher(opmode).doFinal();
            fail(msg);
        } catch (IllegalStateException expected) {
        }

        try {
            createAesCipher(opmode).doFinal(new byte[0]);
            fail(msg);
        } catch (IllegalStateException expected) {
        }

        try {
            createAesCipher(opmode).doFinal(new byte[2 * bs], 0, bs);
            fail(msg);
        } catch (IllegalStateException expected) {
        }

        try {
            createAesCipher(opmode).doFinal(new byte[2 * bs], 0, bs, new byte[2 * bs], 0);
            fail(msg);
        } catch (IllegalStateException expected) {
        }
    }
Helper for testing that Cipher.doFinal() throws IllegalStateException when
initialized in modes other than DECRYPT or ENCRYPT.
common/src/test/java/org/conscrypt/javax/crypto/CipherTest.java