Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

SecretKeyAESTest.java

00001 package edu.virtualschool.jco.test;
00002 
00003 import java.security.spec.InvalidKeySpecException;
00004 
00005 import junit.framework.TestCase;
00006 import edu.virtualschool.jco.JCOGenericBytes;
00007 import edu.virtualschool.jco.JCOFault;
00008 import edu.virtualschool.jco.JCOSealedBytes;
00009 import edu.virtualschool.jco.JCOSecretKeyAES;
00010 import edu.virtualschool.jco.JCOSymmetricKey;
00011 import edu.virtualschool.jwaa.IntervalTimer;
00012 import edu.virtualschool.jwaa.RuntimeFault;
00013 
00014 public class SecretKeyAESTest extends TestCase
00015 {
00016   private final static byte[] key16 = "1234567890123456".getBytes();
00017   private final static byte[] key24 = "123456789012345678901234".getBytes();
00018   private final static byte[] key32 = "12345678901234567890123456789012".getBytes();
00019   
00020   public static void main(String[] args)
00021   {
00022     junit.textui.TestRunner.run(SecretKeyAESTest.class);
00023   }
00024   public SecretKeyAESTest(String arg0)
00025   {
00026     super(arg0);
00027   }
00028   final public void testSecretKey() throws JCOFault
00029   {
00030     checkKey(new JCOSecretKeyAES());
00031   }
00032   final public void testEncoding() throws InvalidKeySpecException
00033   {
00034     JCOSecretKeyAES key = new JCOSecretKeyAES();
00035     assertEquals(key.getFormat(), "RAW");
00036     assertEquals( "AES", key.getAlgorithm());
00037     
00038     JCOSymmetricKey originalKey = new JCOSecretKeyAES(key16);
00039     JCOSymmetricKey newKey = new JCOSecretKeyAES(originalKey.getEncoded());
00040     assertEquals(originalKey.encode(), newKey.encode());
00041     
00042     originalKey = new JCOSecretKeyAES(key24);
00043     newKey = new JCOSecretKeyAES(originalKey.getEncoded());
00044     assertEquals(originalKey.encode(), newKey.encode());
00045     
00046     originalKey = new JCOSecretKeyAES(key32);
00047     newKey = new JCOSecretKeyAES(originalKey.getEncoded());
00048     assertEquals(originalKey.encode(), newKey.encode());
00049   }
00050   final public void testEqualsObject()
00051     throws InvalidKeySpecException
00052   {
00053     JCOSymmetricKey key1 = new JCOSecretKeyAES(key16);
00054     JCOSymmetricKey key2 = new JCOSecretKeyAES(key16);
00055     assertEquals(key1.encode(), key2.encode());
00056     
00057     key1 = new JCOSecretKeyAES(key24);
00058     key2 = new JCOSecretKeyAES(key24);
00059     assertEquals(key1.encode(), key2.encode());
00060     
00061     key1 = new JCOSecretKeyAES(key32);
00062     key2 = new JCOSecretKeyAES(key32);
00063     assertEquals(key1.encode(), key2.encode());
00064   }
00065   final public void testEncryptDecryptObjects()
00066   throws JCOFault
00067   {
00068     String object = "test string";
00069     IntervalTimer t = new IntervalTimer();
00070     JCOSecretKeyAES key = new JCOSecretKeyAES(16);
00071     System.out.println(t + " JCOSecretKeyAES: new JCOSecretKeyAES(16)");
00072     byte[] ciphertext = key.encrypt(object);
00073     System.out.println(t + " JCOSecretKeyAES.encrypt(object)");
00074     Object newObject = key.decrypt(ciphertext);
00075     System.out.println(t + " JCOSecretKeyAES.decrypt(ciphertext)");
00076     assertEquals(object, newObject);
00077     
00078     key = new JCOSecretKeyAES(32);
00079     System.out.println(t + " new JCOSecretKeyAES(24)");
00080     ciphertext = key.encrypt(object);
00081     System.out.println(t + " JCOSecretKeyAES.encrypt(object)");
00082     newObject = key.decrypt(ciphertext);
00083     System.out.println(t + " JCOSecretKeyAES.decrypt(ciphertext)");
00084     assertEquals(object, newObject);
00085     
00086     key = new JCOSecretKeyAES(32);
00087     System.out.println(t + " new JCOSecretKeyAES(32)");
00088     ciphertext = key.encrypt(object);
00089     System.out.println(t + " JCOSecretKeyAES.encrypt(object)");
00090     newObject = key.decrypt(ciphertext);
00091     System.out.println(t + " JCOSecretKeyAES.decrypt(ciphertext)");
00092     assertEquals(object, newObject);
00093   }
00094   final public void testEncryptDecryptBytes()
00095   throws JCOFault
00096   {
00097     String object = "test string";
00098     JCOSecretKeyAES key = new JCOSecretKeyAES();
00099     byte[] input = object.getBytes();
00100     byte[] ciphertext = key.encryptBytes(input);
00101     byte[] cleartext = key.decryptBytes(ciphertext);
00102     assertTrue(JCOGenericBytes.isEqualBytes(input, cleartext));
00103   }
00104   final public void testValidLengths() throws Exception
00105   {
00106 //    throw new IllegalArgumentException("Key length not 128/192/256 bits.");
00107     int[] validLengths = new int[] { 16, 24, 32 };
00108     for (int i = 0; i < validLengths.length; i++)
00109       checkKey(new JCOSecretKeyAES(validLengths[i]));
00110   }
00111   final public void testInvalidLengths() throws Exception
00112   {
00113       int[] invalidLengths = new int[] { -1, 0, 15, 23, 31, 33 };
00114       for (int i = 0; i < invalidLengths.length; i++)
00115       {
00116         try
00117         {
00118           checkKey(new JCOSecretKeyAES(invalidLengths[i]));
00119           assertTrue(invalidLengths[i] + " should not be accepted as valid", false);
00120         }
00121         catch (NegativeArraySizeException e)
00122         {
00123           continue;
00124         }
00125         catch (RuntimeFault e)
00126         {
00127           continue;
00128         }
00129       }
00130   }
00131 
00132   final static void checkKey(JCOSymmetricKey key)
00133   throws JCOFault
00134   {
00135     String input = "some cleartext";
00136     JCOSealedBytes ciphertext = key.seal(input);
00137     Object cleartext = key.unseal(ciphertext);
00138     assertEquals(cleartext, input);
00139   }
00140 }