package org.yi.happy.archive.block.encoder;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;
import org.yi.happy.archive.block.Block;
import org.yi.happy.archive.crypto.CipherFactory;
import org.yi.happy.archive.crypto.DigestFactory;
import org.yi.happy.archive.test_data.TestData;

/**
 * tests for block encode
 */
public class BlockEncoderBlobTest {
    /**
     * encode a block using default block encoding
     * 
     * @throws IOException
     */
    @Test
    public void testContentEncode() throws IOException {
        Block in = TestData.CLEAR_CONTENT.getBlock();

        BlockEncoder e = new BlockEncoderBlob(DigestFactory
                .getProvider("sha-256"), CipherFactory
                .getProvider("rijndael256-256-cbc"));
        BlockEncoderResult r = e.encode(in);
        Block out = r.getBlock();

        Block want = TestData.KEY_BLOB.getBlock();
        Assert.assertArrayEquals(want.asBytes(), out.asBytes());
        Assert.assertEquals(TestData.KEY_BLOB.getFullKey(), r.getKey());
    }

    /**
     * encode a block with non-default settings
     * 
     * @throws IOException
     */
    @Test
    public void testEncodeNonDefault() throws IOException {
        Block in = TestData.CLEAR_CONTENT.getBlock();

        BlockEncoder be = new BlockEncoderBlob(DigestFactory
                .getProvider("sha-1"), CipherFactory.getProvider("aes-192-cbc"));
        BlockEncoderResult r = be.encode(in);
        Block have = r.getBlock();

        TestData d = TestData.KEY_BLOB_SHA1_AES192;
        Block want = d.getBlock();
        assertEquals(d.getFullKey(), r.getKey());
        assertArrayEquals(want.asBytes(), have.asBytes());
    }

    /**
     * encode with all default settings
     * 
     * @throws IOException
     */
    @Test
    public void testEncodeDefault() throws IOException {
        Block in = TestData.CLEAR_CONTENT.getBlock();

        BlockEncoder be = new BlockEncoderBlob(DigestFactory
                .getProvider("sha-256"), CipherFactory
                .getProvider("aes-128-cbc"));
        BlockEncoderResult r = be.encode(in);
        Block have = r.getBlock();

        TestData d = TestData.KEY_BLOB_AES128;
        Block want = d.getBlock();
        assertEquals(d.getFullKey(), r.getKey());
        assertArrayEquals(want.asBytes(), have.asBytes());
    }
}

@@ -337,29 +337,131 @@
 .crypto.
-DigestFactory
+CipherProvider;%0Aimport org.yi.happy.archive.crypto.DigestFactory;%0Aimport org.yi.happy.archive.crypto.DigestProvider
 ;%0Aimport
@@ -847,107 +847,18 @@
 lob(
-DigestFactory%0A                .getProvider(%22
 sha
--
 256
-%22
+(
 ), 
-CipherFactory%0A                .getProvider(%22
 rijn
@@ -868,17 +868,13 @@
 l256
--256-cbc%22
+_256(
 ));%0A
@@ -910,32 +910,33 @@
 = e.encode(in);%0A
+%0A
         Block ou
@@ -1365,32 +1365,36 @@
 ENT.getBlock();%0A
+    
 %0A        BlockEn
@@ -1429,100 +1429,23 @@
 lob(
-DigestFactory%0A                .getProvider(%22sha-1%22), CipherFactory.getProvider(%22
+sha1(), 
 aes
--
 192
--cbc%22
+(
 ));%0A
@@ -1517,33 +1517,37 @@
 = r.getBlock();%0A
+    
 %0A
-
         TestData
@@ -1772,32 +1772,70 @@
 default settings
+ (although they are not defaults now).
 %0A     * %0A     * 
@@ -1975,32 +1975,36 @@
 ENT.getBlock();%0A
+    
 %0A        BlockEn
@@ -2039,119 +2039,25 @@
 lob(
-DigestFactory%0A                .getProvider(%22
 sha
--
 256
-%22
+(
 ), 
-CipherFactory%0A                .getProvider(%22
 aes
--
 128
--cbc%22
+(
 ));%0A
@@ -2129,33 +2129,37 @@
 = r.getBlock();%0A
+    
 %0A
-
         TestData
@@ -2266,32 +2266,32 @@
 ), r.getKey());%0A
-
         assertAr
@@ -2338,11 +2338,526 @@
 );%0A    %7D
+%0A%0A    private CipherProvider rijndael256_256() %7B%0A        return CipherFactory.getProvider(%22rijndael256-256-cbc%22);%0A    %7D%0A%0A    private CipherProvider aes192() %7B%0A        return CipherFactory.getProvider(%22aes-192-cbc%22);%0A    %7D%0A%0A    private DigestProvider sha1() %7B%0A        return DigestFactory.getProvider(%22sha-1%22);%0A    %7D%0A%0A    private CipherProvider aes128() %7B%0A        return CipherFactory.getProvider(%22aes-128-cbc%22);%0A    %7D%0A%0A    private DigestProvider sha256() %7B%0A        return DigestFactory.getProvider(%22sha-256%22);%0A    %7D
 %0A%7D%0A

make a test easier to read.