package org.owasp.esapi.crypto;

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
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 CipherSpec cipherSpec = null;
    private Cipher encryptor = null;
    private Cipher decryptor = null;
    private IvParameterSpec ivSpec = null;
    

    @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");
        decryptor = 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;
            raw = encryptor.doFinal("Hello".getBytes("UTF8"));
            CipherText ct = new CipherText(cipherSpec, raw);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        } catch (EncryptionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        }
    }

    @Test
    public final void testAsCipherText() {
        fail("Not yet implemented"); // TODO
    }

}

@@ -2805,44 +2805,504 @@
     
-fail(%22Not yet implemented%22); // TODO
+try %7B%0A            CipherText ct = ESAPI.encryptor().encrypt( new PlainText(%22Hello%22) );%0A            CipherTextSerializer cts = new CipherTextSerializer( ct );%0A            CipherText result = cts.asCipherText();%0A            assertTrue( ct.equals(result) );%0A            PlainText pt = ESAPI.encryptor().decrypt(result);%0A            assertTrue( %22Hello%22.equals( pt.toString() ) );%0A        %7D catch (EncryptionException e) %7B%0A            fail(%22Caught EncryptionException; exception msg: %22 + e);%0A        %7D
 %0A   

Implement unimplemented test.