<commit_before>package org.owasp.esapi.crypto;

import static org.junit.Assert.*;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.errors.EncryptionException;

public class CipherTextSerializerTest {
    private Cipher encryptor = null;
    private IvParameterSpec ivSpec = null;  // Note: FindBugs reports false positive
                                            // about this being unread field.

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
        encryptor = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] ivBytes = null;
        ivBytes = ESAPI.randomizer().getRandomBytes(encryptor.getBlockSize());
        ivSpec = new IvParameterSpec(ivBytes);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testAsSerializedByteArray() {
        CipherSpec cipherSpec = new CipherSpec(encryptor, 128);
        cipherSpec.setIV(ivSpec.getIV());
        SecretKey key;
        try {
            key = CryptoHelper.generateSecretKey(cipherSpec.getCipherAlgorithm(), 128);
            encryptor.init(Cipher.ENCRYPT_MODE, key, ivSpec);

            byte[] raw = encryptor.doFinal("Hello".getBytes("UTF8"));
            CipherText ct = ESAPI.encryptor().encrypt(key, new PlainText("Hello") );
            assertTrue( ct != null );   // Here to eliminate false positive from FindBugs.
            CipherTextSerializer cts = new CipherTextSerializer( ct );
            byte[] serializedBytes = cts.asSerializedByteArray();
            CipherText result = CipherText.fromPortableSerializedBytes(serializedBytes);
            PlainText pt = ESAPI.encryptor().decrypt(key, result);
            assertTrue( "Hello".equals( pt.toString() ) );
        } catch (Exception e) {
            fail("Test failed: Caught exception: " + e.getClass().getName() + "; msg was: " + e);
            e.printStackTrace(System.err);
        }
    }

    @Test
    public final void testAsCipherText() {
        try {
            CipherText ct = ESAPI.encryptor().encrypt( new PlainText("Hello") );
            CipherTextSerializer cts = new CipherTextSerializer( ct );
            CipherText result = cts.asCipherText();<commit_msg>Added a System.out.flush() in teardown (in case certain Exceptions or AssertionErrors thrown, try to preserve unflushed output), and added 2 lines of output in regards to which test is running.<commit_after>package org.owasp.esapi.crypto;

import static org.junit.Assert.*;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.errors.EncryptionException;

public class CipherTextSerializerTest {
    private Cipher encryptor = null;
    private IvParameterSpec ivSpec = null;  // Note: FindBugs reports false positive
                                            // about this being unread field.

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
        encryptor = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] ivBytes = null;
        ivBytes = ESAPI.randomizer().getRandomBytes(encryptor.getBlockSize());
        ivSpec = new IvParameterSpec(ivBytes);
    }

    @After
    public void tearDown() throws Exception {
    	System.out.flush();
    }

    @Test
    public final void testAsSerializedByteArray() {
    	System.out.println("CipherTextSerializerTest.testAsSerializedByteArray() ...");
        CipherSpec cipherSpec = new CipherSpec(encryptor, 128);
        cipherSpec.setIV(ivSpec.getIV());
        SecretKey key;
        try {
            key = CryptoHelper.generateSecretKey(cipherSpec.getCipherAlgorithm(), 128);
            encryptor.init(Cipher.ENCRYPT_MODE, key, ivSpec);

            byte[] raw = encryptor.doFinal("Hello".getBytes("UTF8"));
            CipherText ct = ESAPI.encryptor().encrypt(key, new PlainText("Hello") );
            assertTrue( ct != null );   // Here to eliminate false positive from FindBugs.
            CipherTextSerializer cts = new CipherTextSerializer( ct );
            byte[] serializedBytes = cts.asSerializedByteArray();
            CipherText result = CipherText.fromPortableSerializedBytes(serializedBytes);
            PlainText pt = ESAPI.encryptor().decrypt(key, result);
            assertTrue( "Hello".equals( pt.toString() ) );
        } catch (Exception e) {
            fail("Test failed: Caught exception: " + e.getClass().getName() + "; msg was: " + e);
            e.printStackTrace(System.err);
        }
    }

    @Test
    public final void testAsCipherText() {
        try {
        	System.out.println("CipherTextSerializerTest.testAsCipherText() ...");
            CipherText ct = ESAPI.encryptor().encrypt( new PlainText("Hello") );
            CipherTextSerializer cts = new CipherTextSerializer( ct );
            CipherText result = cts.asCipherText();
{'fraction_non_alphanumeric': 0.1036849710982659, 'fraction_numerical': 0.0030708092485549135, 'mean_word_length': 2.8265376641326885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4740641', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1532, 'n_words': 369}