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

KeyPairTest.java

00001 /*
00002  * Created on Jun 19, 2003
00003  *
00004  * To change the template for this generated file go to
00005  * Window>Preferences>Java>Code Generation>Code and Comments
00006  */
00007 package edu.virtualschool.jco.test;
00008 
00009 import junit.framework.TestCase;
00010 import edu.virtualschool.jco.JCOFault;
00011 import edu.virtualschool.jco.JCOKey;
00012 import edu.virtualschool.jco.JCOKeyPair;
00013 import edu.virtualschool.jco.JCOPrivateKey;
00014 import edu.virtualschool.jco.JCOPublicKey;
00015 import edu.virtualschool.jco.JCOSignedBytes;
00016 import edu.virtualschool.jwaa.IntervalTimer;
00017 
00024 public class KeyPairTest extends TestCase
00025 {
00031   final JCOKeyPair keypair;
00032     
00033   public static void main(String[] args)
00034   {
00035     junit.textui.TestRunner.run(KeyPairTest.class);
00036   }
00037   public KeyPairTest(String arg0)
00038   {
00039     super(arg0);
00040     this.keypair = new JCOKeyPair();
00041   }
00042   public void testKeyPair() 
00043     throws JCOFault
00044   {
00045     int[] validLengths = new int[] { 40/8, 1026/8, 2048/8 };
00046     for (int j = 0; j < validLengths.length; j++)
00047     {
00048       int i = validLengths[j];
00049 //      JCOKeyPair pair = null;
00050       try
00051       {
00052         checkKeyOfLength(i);
00053       }
00054       catch (JCOFault e)
00055       {
00056         System.out.println("key length error:"+i);
00057       }
00058     }
00059 //    int[] invalidLengths = new int[] { (40-1)/8, (2048+1)/8, 0 };
00060 //    for (int j = 0; j < 10; j++)
00061 //    {
00062 //      int length = invalidLengths[j];
00063 //      try
00064 //      {
00065 //        JCOKeyPair pair1 = new JCOKeyPair(length);
00066 //        assertTrue("keylength " + length + " should not be accepted", false);
00067 //      }
00068 //      catch (RuntimeFault e) {}
00069 //    }
00070 //  }
00071   }
00072   private static void checkKeyOfLength(int length) 
00073     throws JCOFault
00074   {
00075     String object = "123456789012345678901234567890";
00076     
00077     IntervalTimer t = new IntervalTimer();
00078     JCOKeyPair pair = new JCOKeyPair(length);
00079     System.out.println(t + "  new JCOKeyPair("+length+")");
00080     JCOPrivateKey privateKey = pair.getPrivate();
00081     JCOPublicKey publicKey = pair.getPublic();
00082 
00083     JCOSignedBytes signedObject = privateKey.sign(object);
00084     System.out.println(t + "  JCOPrivateKey.sign");
00085     Object unsignedObject = publicKey.verify(signedObject);
00086     System.out.println(t + "  JCOPublicKey.verify");
00087     assertEquals(object, unsignedObject);
00088   }
00089   public void testKeyEncodeDecode()
00090     throws Exception
00091   {
00092     checkEncodeDecode(keypair.getPublic());
00093     checkEncodeDecode(keypair.getPrivate());
00094   }
00095   private void checkEncodeDecode(JCOKey key)
00096     throws Exception
00097   {
00098     byte[] keyBytes = key.getEncoded();
00099     JCOKey decodedKey = null;
00100     if (key instanceof JCOPublicKey)
00101       decodedKey = new JCOPublicKey(keyBytes);
00102     else if (key instanceof JCOPrivateKey)
00103       decodedKey = new JCOPrivateKey(keyBytes);
00104     else 
00105       throw new Exception("unexpected key type:"+key);
00106     assertEquals(key.encode(), decodedKey.encode());
00107   }
00108   public void testSignature() throws Exception
00109   {
00110     JCOPrivateKey privateKey = keypair.getPrivate();
00111     JCOPublicKey publicKey = keypair.getPublic();
00112 
00113     String originalObject = "foobar";
00114     IntervalTimer t = new IntervalTimer();
00115     JCOSignedBytes signedObject = privateKey.sign(originalObject);
00116     System.out.println(t + "  JCOPrivateKey.sign(object)");
00117     Object verifiedObject = publicKey.verify(signedObject);
00118     System.out.println(t + "  JCOPublicKey.veritfy(signedObject)");
00119     assertEquals(originalObject, verifiedObject);
00120   }
00121 }