harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r418191 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api: java.injected/java/security/serialization/ java/org/apache/harmony/security/tests/java/security/serialization/
Date Fri, 30 Jun 2006 07:27:09 GMT
Author: smishura
Date: Fri Jun 30 00:27:08 2006
New Revision: 418191

URL: http://svn.apache.org/viewvc?rev=418191&view=rev
Log:
Refactoring KeyPairTest to be provider independent

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/serialization/
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/serialization/KeyPairTest.java
      - copied, changed from r418179, incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/serialization/KeyPairTest.java
Removed:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/serialization/KeyPairTest.java

Copied: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/serialization/KeyPairTest.java
(from r418179, incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/serialization/KeyPairTest.java)
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/serialization/KeyPairTest.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/serialization/KeyPairTest.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/serialization/KeyPairTest.java&r1=418179&r2=418191&rev=418191&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/serialization/KeyPairTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/serialization/KeyPairTest.java
Fri Jun 30 00:27:08 2006
@@ -19,147 +19,80 @@
 * @version $Revision$
 */
 
-package java.security.serialization;
+package org.apache.harmony.security.tests.java.security.serialization;
 
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
+import java.io.Serializable;
 import java.security.Key;
 import java.security.KeyPair;
-import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.spec.InvalidKeySpecException;
 import java.util.Arrays;
 
-import org.apache.harmony.security.tests.support.SpiEngUtils;
-import org.apache.harmony.security.tests.support.TestKeyPair;
-import org.apache.harmony.security.tests.support.TestUtils;
-
 import junit.framework.TestCase;
 
+import org.apache.harmony.security.tests.support.PrivateKeyStub;
+import org.apache.harmony.security.tests.support.PublicKeyStub;
+import org.apache.harmony.testframework.serialization.SerializationTest;
+import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
+
 
 /**
  * Tests for <code>KeyPair</code> serialization
- * 
  */
 public class KeyPairTest extends TestCase {
-    /**
-     * KeyFactory algorithm name used during testing
-     */
-    private static String algorithmName;
-    /**
-     * Test key pairs holder
-     */
-    private static TestKeyPair tkp;
-    /**
-     * Set to true if test init is OK
-     */
-    private static boolean initOk = false;
-    static {
-        String[] tryAlg = {"DSA", "RSA"};
-        for (int i=0; i<tryAlg.length; i++) {
-            try {
-                tkp = new TestKeyPair(tryAlg[i]);
-                algorithmName = tryAlg[i];
-                initOk = true;
-                break;
-            } catch (NoSuchAlgorithmException e) {
-                // try another algorithm
-            }
-        }
-    }
 
-    /**
-     * Constructor for KeyPairTest.
-     * @param name
-     * @throws NoSuchAlgorithmException
-     */
-    public KeyPairTest(String name) throws NoSuchAlgorithmException {
-        super(name);
-    }
+    // key pair for testing
+    private KeyPair keyPair;
+
+    protected void setUp() throws Exception {
+        PrivateKeyStub privateKey = new PrivateKeyStub("privateAlgorithm",
+                "privateFormat", new byte[] { 0x00, 0x05, 0x10 });
+        PublicKeyStub publicKey = new PublicKeyStub("publicAlgorithm",
+                "publicFormat", new byte[] { 0x01, 0x02, 0x12 });
 
-    //
-    // Tests
-    //
+        keyPair = new KeyPair(publicKey, privateKey);
+    }
 
     /**
-     * Test #2 for <code>KeyPair</code> serialization/deserialization.<br>
-     * File streams used.
-     *
-     * Assertion: deserialized object must contain the same keys as original
-     * 
-     * @throws NoSuchAlgorithmException
-     * @throws IOException
-     * @throws ClassNotFoundException
+     * Tests serialization compatibility
      */
-    public final void testSerialization02()
-            throws NoSuchAlgorithmException,
-                   InvalidKeySpecException,
-                   IOException,
-                   ClassNotFoundException {
-        if (!initOk) {
-            System.err.println(getName() +
-                    ": no KeyFactory for DSA or RSA - test skipped");
-            return;
-        }
-        // Create object to be compared with deserialized one
-        KeyPair kp = new KeyPair(tkp.getPublic(), tkp.getPrivate());
-        // This testcase uses File stream
-        String fileName = SpiEngUtils.getFileName(TestUtils.TEST_ROOT,
-            "java/security/serialization/KeyPair." + algorithmName + ".dat");
-        deserializeAndCheck(kp, new FileInputStream(fileName));
-    }
+    public void testSerializationCompatibility() throws Exception {
 
+        SerializationTest.verifyGolden(this, keyPair, comparator);
+    }
+    
     /**
-     * Test case end template - deserialization and checks
-     *
-     * @param kp
-     * reference <code>KeyPair</code> object
-     * @param os
-     * Deserialization <code>InputStream</code>
+     * Tests serialization/deserialization
      */
-    private void deserializeAndCheck(KeyPair kp, InputStream is)
-            throws IOException,
-                   ClassNotFoundException {
-        // deserialize our object
-        ObjectInputStream ois = new ObjectInputStream(is);
-        KeyPair kp1 = null;
-        try {
-            kp1 = (KeyPair)ois.readObject();
-        } finally {
-            ois.close();
-        }
+    public void testSerializationSelf() throws Exception {
 
-        // check result: compare public keys
-        Key kpKey = kp.getPublic();
-        Key kp1Key = kp1.getPublic();
-
-        assertTrue("kp key is public",
-                kpKey instanceof PublicKey);
-        assertTrue("kp1 key is public",
-                kp1Key instanceof PublicKey);
-        assertTrue("public algorithm",
-                kpKey.getAlgorithm().equals(kp1Key.getAlgorithm()));
-        assertTrue("public format",
-                kpKey.getFormat().equals(kp1Key.getFormat()));
-        assertTrue("public encoded",
-                Arrays.equals(kpKey.getEncoded(), kp1Key.getEncoded()));
-
-        // check result: compare private keys
-        kpKey = kp.getPrivate();
-        kp1Key = kp1.getPrivate();
-
-        assertTrue("kp key is private",
-                kpKey instanceof PrivateKey);
-        assertTrue("kp1 key is private",
-                kp1Key instanceof PrivateKey);
-        assertTrue("private algorithm",
-                kpKey.getAlgorithm().equals(kp1Key.getAlgorithm()));
-        assertTrue("private format",
-                kpKey.getFormat().equals(kp1Key.getFormat()));
-        assertTrue("private encoded",
-                Arrays.equals(kpKey.getEncoded(), kp1Key.getEncoded()));
+        SerializationTest.verifySelf(keyPair, comparator);
     }
+
+
+    // comparator for KeyPair objects
+    private static SerializableAssert comparator = new SerializableAssert(){
+        public void assertDeserialized(Serializable reference, Serializable test) {
+
+            // check result: compare public keys
+            Key key1 = ((KeyPair)test).getPublic();
+            Key key2 = ((KeyPair)reference).getPublic();
+
+            assertEquals("PublicKey class", key1.getClass(), key2.getClass());
+            assertEquals("PublicKey algorithm", key1.getAlgorithm(), key2
+                    .getAlgorithm());
+            assertEquals("PublicKey format", key1.getFormat(), key2.getFormat());
+            assertTrue("PublicKey encoded", Arrays.equals(key1.getEncoded(),
+                    key2.getEncoded()));
+
+            // check result: compare private keys
+            key1 = ((KeyPair)test).getPrivate();
+            key2 = ((KeyPair)reference).getPrivate();
+
+            assertEquals("PrivateKey class", key1.getClass(), key2.getClass());
+            assertEquals("PrivateKey algorithm", key1.getAlgorithm(), key2
+                    .getAlgorithm());
+            assertEquals("PrivateKey format", key1.getFormat(), key2.getFormat());
+            assertTrue("PrivateKey encoded", Arrays.equals(key1.getEncoded(),
+                    key2.getEncoded()));
+        }
+    };
 }



Mime
View raw message