Home | History | Annotate | Download | only in cert
      1 package tests.security.cert;
      2 
      3 import junit.framework.TestCase;
      4 
      5 import org.apache.harmony.security.tests.support.cert.MyCertificate;
      6 import org.apache.harmony.security.tests.support.cert.TestUtils;
      7 import org.apache.harmony.security.tests.support.cert.MyCertificate.MyCertificateRep;
      8 
      9 import java.io.ObjectStreamException;
     10 import java.security.cert.Certificate;
     11 import java.util.Arrays;
     12 
     13 public class CertificateCertificateRepTest extends TestCase {
     14 
     15     private static final byte[] testEncoding = new byte[] { (byte) 1, (byte) 2,
     16             (byte) 3, (byte) 4, (byte) 5 };
     17 
     18     protected void setUp() throws Exception {
     19         super.setUp();
     20     }
     21 
     22     protected void tearDown() throws Exception {
     23         super.tearDown();
     24     }
     25 
     26     /**
     27      * Test for
     28      * <code>Certificate.CertificateRep(String type, byte[] data)</code>
     29      * method<br>
     30      */
     31     public final void testCertificateCertificateRep() {
     32         MyCertificate c1 = new MyCertificate("TEST_TYPE", testEncoding);
     33         MyCertificateRep rep = c1.new MyCertificateRep("TEST_TYPE", new byte[] {
     34                 (byte) 1, (byte) 2, (byte) 3 });
     35 
     36         assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) 2, (byte) 3 },
     37                 rep.getData()));
     38         assertEquals("TEST_TYPE", rep.getType());
     39 
     40         try {
     41             c1.new MyCertificateRep(null, null);
     42         } catch (Exception e) {
     43             fail("Unexpected exeption " + e.getMessage());
     44         }
     45 
     46         try {
     47             MyCertificate.MyCertificateRep rep1 = c1.new MyCertificateRep(
     48                     "X509", TestUtils.getX509Certificate_v3());
     49             assertEquals("X509", rep1.getType());
     50             assertTrue(Arrays.equals(TestUtils.getX509Certificate_v3(), rep1.getData()));
     51         } catch (Exception e) {
     52             fail("Unexpected exeption " + e.getMessage());
     53         }
     54     }
     55 
     56     /**
     57      * Test for <code>readResolve()</code> method<br>
     58      */
     59     public final void testReadResolve() {
     60         MyCertificate c1 = new MyCertificate("TEST_TYPE", testEncoding);
     61         MyCertificateRep rep = c1.new MyCertificateRep("TEST_TYPE", new byte[] {
     62                 (byte) 1, (byte) 2, (byte) 3 });
     63 
     64         try {
     65             rep.readResolve();
     66             fail("ObjectStreamException expected");
     67         } catch (ObjectStreamException e) {
     68             // expected
     69         }
     70 
     71         MyCertificateRep rep1 = c1.new MyCertificateRep("X509", TestUtils
     72                 .getX509Certificate_v3());
     73         try {
     74             Certificate obj = (Certificate) rep1.readResolve();
     75             assertEquals("0.3.5", obj.getPublicKey().getAlgorithm());
     76             assertEquals("X.509", obj.getPublicKey().getFormat());
     77             assertEquals("X.509", obj.getType());
     78         } catch (ObjectStreamException e) {
     79             fail("Unexpected ObjectStreamException " + e.getMessage());
     80         }
     81     }
     82 }
     83