diff options
4 files changed, 77 insertions, 183 deletions
diff --git a/luni/src/main/java/org/apache/harmony/security/provider/cert/X509CertImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/cert/X509CertImpl.java index 776677e..a144727 100644 --- a/luni/src/main/java/org/apache/harmony/security/provider/cert/X509CertImpl.java +++ b/luni/src/main/java/org/apache/harmony/security/provider/cert/X509CertImpl.java @@ -402,7 +402,7 @@ public final class X509CertImpl extends X509Certificate { throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { - if (getSigAlgName().endsWith("withRSA")) { + if (getSigAlgName().endsWith("withRSA") && sigProvider == null) { fastVerify(key); return; } diff --git a/luni/src/test/java/tests/security/cert/CertificateTest.java b/luni/src/test/java/tests/security/cert/CertificateTest.java index 1fc0c68..6570416 100644 --- a/luni/src/test/java/tests/security/cert/CertificateTest.java +++ b/luni/src/test/java/tests/security/cert/CertificateTest.java @@ -22,13 +22,6 @@ package tests.security.cert; - - -import dalvik.annotation.AndroidOnly; -import dalvik.annotation.KnownFailure; - -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectStreamException; @@ -39,13 +32,14 @@ import java.security.NoSuchProviderException; import java.security.Provider; import java.security.PublicKey; import java.security.Security; +import java.security.Signature; import java.security.SignatureException; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.util.Arrays; - +import junit.framework.TestCase; import org.apache.harmony.security.tests.support.cert.MyCertificate; import org.apache.harmony.security.tests.support.cert.MyFailingCertificate; import org.apache.harmony.security.tests.support.cert.TestUtils; @@ -62,28 +56,17 @@ public class CertificateTest extends TestCase { private static final byte[] testEncoding = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 }; - // - // Tests - // - /** * Test for <code>Certificate(String type)</code> method<br> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") - public final void testCertificate() { - try { - Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); - assertTrue(Arrays.equals(testEncoding, c1.getEncoded())); - assertEquals("TEST", c1.getPublicKey().getAlgorithm()); - assertTrue(Arrays.equals( - new byte[] { (byte) 1, (byte) 2, (byte) 3 }, c1 - .getPublicKey().getEncoded())); - assertEquals("TEST_FORMAT", c1.getPublicKey().getFormat()); - assertEquals("TEST_TYPE", c1.getType()); - } catch (CertificateEncodingException e) { - fail("Unexpected CertificateEncodingException " + e.getMessage()); - } + public final void testCertificate() throws Exception { + Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); + assertTrue(Arrays.equals(testEncoding, c1.getEncoded())); + assertEquals("TEST", c1.getPublicKey().getAlgorithm()); + assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) 2, (byte) 3 }, + c1.getPublicKey().getEncoded())); + assertEquals("TEST_FORMAT", c1.getPublicKey().getFormat()); + assertEquals("TEST_TYPE", c1.getType()); } /** @@ -91,8 +74,6 @@ public class CertificateTest extends TestCase { * Assertion: returns hash of the <code>Certificate</code> instance * @throws CertificateEncodingException */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testHashCode() throws CertificateEncodingException { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); Certificate c2 = new MyCertificate("TEST_TYPE", testEncoding); @@ -108,8 +89,6 @@ public class CertificateTest extends TestCase { * Test for <code>hashCode()</code> method<br> * Assertion: hash code of equal objects should be the same */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testHashCodeEqualsObject() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); Certificate c2 = new MyCertificate("TEST_TYPE", testEncoding); @@ -123,8 +102,6 @@ public class CertificateTest extends TestCase { * Test for <code>getType()</code> method<br> * Assertion: returns this certificate type */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testGetType() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); assertEquals("TEST_TYPE", c1.getType()); @@ -134,8 +111,6 @@ public class CertificateTest extends TestCase { * Test #1 for <code>equals(Object)</code> method<br> * Assertion: object equals to itself */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testEqualsObject01() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); assertTrue(c1.equals(c1)); @@ -146,8 +121,6 @@ public class CertificateTest extends TestCase { * Assertion: object equals to other <code>Certificate</code> * instance with the same state */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testEqualsObject02() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); Certificate c2 = new MyCertificate("TEST_TYPE", testEncoding); @@ -158,8 +131,6 @@ public class CertificateTest extends TestCase { * Test for <code>equals(Object)</code> method<br> * Assertion: object not equals to <code>null</code> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testEqualsObject03() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); assertFalse(c1.equals(null)); @@ -170,42 +141,36 @@ public class CertificateTest extends TestCase { * Assertion: object not equals to other which is not * instance of <code>Certificate</code> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testEqualsObject04() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); assertFalse(c1.equals("TEST_TYPE")); } - // // the following tests just call methods // that are abstract in <code>Certificate</code> // (So they just like signature tests) - // /** * This test just calls <code>getEncoded()</code> method<br> * @throws CertificateException */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") - @KnownFailure("Assertion does not evaluate to true... Works in javax.Certificate") public final void testGetEncoded() throws CertificateException { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); assertNotNull(c1.getEncoded()); + assertTrue(Arrays.equals(testEncoding,c1.getEncoded())); - assertTrue(Arrays.equals(TestUtils.rootCert.getBytes(),cert.getEncoded())); - - byte[] b = TestUtils.rootCert.getBytes(); - - b[4] = (byte) 200; + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + byte[] expectedEncoding = cert.getEncoded(); + Certificate actual = cf.generateCertificate(new ByteArrayInputStream(expectedEncoding)); + byte[] actualEncoding = actual.getEncoded(); + assertTrue(Arrays.equals(expectedEncoding, actualEncoding)); + assertFalse(expectedEncoding[4] == 200); + expectedEncoding[4] = (byte) 200; try { - CertificateFactory cf = CertificateFactory.getInstance("X.509"); - ByteArrayInputStream stream = new ByteArrayInputStream(b); - cert = cf.generateCertificate(stream); - } catch (CertificateException e) { - //ok + cf.generateCertificate(new ByteArrayInputStream(expectedEncoding)); + fail(); + } catch (CertificateException expected) { } } @@ -218,8 +183,6 @@ public class CertificateTest extends TestCase { * @throws NoSuchProviderException * @throws SignatureException */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testVerifyPublicKey() throws InvalidKeyException, CertificateException, @@ -239,8 +202,6 @@ public class CertificateTest extends TestCase { * @throws NoSuchProviderException * @throws SignatureException */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testVerifyPublicKeyString() throws InvalidKeyException, CertificateException, @@ -254,8 +215,6 @@ public class CertificateTest extends TestCase { /** * This test just calls <code>toString()</code> method<br> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testToString() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); c1.toString(); @@ -264,8 +223,6 @@ public class CertificateTest extends TestCase { /** * This test just calls <code>testGetPublicKey()</code> method<br> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testGetPublicKey() { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); c1.getPublicKey(); @@ -274,18 +231,10 @@ public class CertificateTest extends TestCase { /** * This test just calls <code>writeReplace()</code> method<br> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") - public final void testWriteReplace() { + public final void testWriteReplace() throws Exception { MyCertificate c1 = new MyCertificate("TEST_TYPE", testEncoding); - - try { - Object obj = c1.writeReplace(); - assertTrue(obj.toString().contains( - "java.security.cert.Certificate$CertificateRep")); - } catch (ObjectStreamException e) { - fail("Unexpected ObjectStreamException " + e.getMessage()); - } + Object obj = c1.writeReplace(); + assertTrue(obj.toString().contains("java.security.cert.Certificate$CertificateRep")); } public class MyModifiablePublicKey implements PublicKey { @@ -353,7 +302,7 @@ public class MyModifiablePublicKey implements PublicKey { private Provider wrongProvider; - private Provider useFulProvider; + private Provider usefulProvider; public void setUp() throws Exception { super.setUp(); @@ -361,7 +310,7 @@ public class MyModifiablePublicKey implements PublicKey { cert = TestUtils.rootCertificateSS; CertificateFactory cf = CertificateFactory.getInstance("X.509"); wrongProvider = cf.getProvider(); - useFulProvider = Security.getProviders("Signature.sha1WithRSAEncryption")[0]; + usefulProvider = Signature.getInstance("SHA1WithRSA").getProvider(); } /** @@ -373,21 +322,19 @@ public class MyModifiablePublicKey implements PublicKey { * @throws NoSuchProviderException * @throws SignatureException */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testVerifyPublicKeyString2() throws InvalidKeyException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException { // real test - cert.verify(cert.getPublicKey(), useFulProvider.getName()); + cert.verify(cert.getPublicKey(), usefulProvider.getName()); // Exception tests try { cert.verify(cert.getPublicKey(), "UnknownProvider"); - } catch (NoSuchProviderException e) { - // ok + fail(); + } catch (NoSuchProviderException expected) { } // This test has side effects affecting all other tests running later @@ -399,8 +346,8 @@ public class MyModifiablePublicKey implements PublicKey { // // try { // cert.verify(cert.getPublicKey(), wrongProvider.getName()); - // } catch (NoSuchAlgorithmException e) { - // // ok + // fail(); + // } catch (NoSuchAlgorithmException expected) { // } // // Security.insertProviderAt(wrongProvider, oldPosition); @@ -412,14 +359,14 @@ public class MyModifiablePublicKey implements PublicKey { try { cert.verify(tamperedKey, provs[0].getName()); - } catch (SignatureException e) { - // ok + fail(); + } catch (SignatureException expected) { } try { cert.verify(c1.getPublicKey(), provs[0].getName()); - } catch (InvalidKeyException e) { - // ok + fail(); + } catch (InvalidKeyException expected) { } */ } @@ -435,11 +382,8 @@ public class MyModifiablePublicKey implements PublicKey { * @throws IOException * @throws InvalidAlgorithmParameterException */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") - public final void testVerifyPublicKey2() throws InvalidKeyException, - CertificateException, NoSuchAlgorithmException, - NoSuchProviderException, SignatureException, InvalidAlgorithmParameterException, IOException { + public final void testVerifyPublicKey2() + throws InvalidKeyException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, InvalidAlgorithmParameterException, IOException { Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); c1.verify(null); @@ -449,44 +393,37 @@ public class MyModifiablePublicKey implements PublicKey { PublicKey k = cert.getPublicKey(); MyModifiablePublicKey changedEncoding = new MyModifiablePublicKey(k); - changedEncoding - .setEncoding(new byte[cert.getEncoded().length - 1]); + changedEncoding.setEncoding(new byte[cert.getEncoded().length - 1]); try { cert.verify(c1.getPublicKey()); - fail("expected InvalidKeyException"); - } catch (InvalidKeyException e) { - // ok + fail(); + } catch (InvalidKeyException expected) { } try { cert.verify(changedEncoding); - fail("Exception expected"); - } catch (Exception e) { - // ok + fail(); + } catch (Exception expected) { } } /** * This test just calls <code>writeReplace()</code> method<br> */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public final void testWriteReplace2() { MyCertificate c1 = new MyFailingCertificate("TEST_TYPE", testEncoding); try { - Object obj = c1.writeReplace(); - } catch (ObjectStreamException e) { - //ok + c1.writeReplace(); + fail(); + } catch (ObjectStreamException expected) { } } /** * serialization/deserialization compatibility. */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public void testSerializationSelf() throws Exception { TestUtils.initCertPathSSCertChain(); @@ -496,8 +433,6 @@ public class MyModifiablePublicKey implements PublicKey { /** * serialization/deserialization compatibility with RI. */ - @AndroidOnly("Gets security providers with specific signature algorithm: " + - "Security.getProviders(\"Signature.sha1WithRSAEncryption\")") public void testSerializationCompatibility() throws Exception { //create test file (once) // SerializationTest.createGoldenFile("device/dalvik/libcore/security/src/test/resources/serialization", this, TestUtils.rootCertificateSS); diff --git a/support/src/test/java/org/apache/harmony/testframework/serialization/SerializationTest.java b/support/src/test/java/org/apache/harmony/testframework/serialization/SerializationTest.java index 68ec773..f20f9de 100644 --- a/support/src/test/java/org/apache/harmony/testframework/serialization/SerializationTest.java +++ b/support/src/test/java/org/apache/harmony/testframework/serialization/SerializationTest.java @@ -40,7 +40,6 @@ import java.util.Collections; import java.util.HashSet; import junit.framework.Assert; import junit.framework.TestCase; -import libcore.util.Objects; /** * Framework for serialization testing. Subclasses only need to override @@ -64,22 +63,6 @@ import libcore.util.Objects; public abstract class SerializationTest extends TestCase { /** - * Property name for the testing mode. - */ - public static final String MODE_KEY = "test.mode"; - - - /** - * Testing mode. - */ - public static String mode = System.getProperty(MODE_KEY); - - /** - * Reference files generation mode switch. - */ - public static final String SERIAL_REFERENCE_MODE = "serial.reference"; - - /** * Key to a system property defining root location of golden files. */ public static final String GOLDEN_PATH = "RESOURCE_DIR"; @@ -88,33 +71,6 @@ public abstract class SerializationTest extends TestCase { "src/test/resources/serialization"); /** - * Parameterized c-tor inherited from superclass. - */ - public SerializationTest(String name) { - super(name); - } - - /** - * Default c-tor inherited from superclass. - */ - public SerializationTest() { - super(); - } - - /** - * Depending on testing mode, produces golden files or performs testing. - */ - @Override - public void runBare() throws Throwable { - - if (Objects.equal(mode, SERIAL_REFERENCE_MODE)) { - produceGoldenFiles(); - } else { - super.runBare(); - } - } - - /** * This is the main working method of this framework. Subclasses must * override it to provide actual objects for testing. * @@ -193,7 +149,7 @@ public abstract class SerializationTest extends TestCase { * Serializes specified object to an output stream. */ public static void putObjectToStream(Object obj, OutputStream os) - throws IOException { + throws IOException { ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(obj); oos.flush(); @@ -203,8 +159,8 @@ public abstract class SerializationTest extends TestCase { /** * Deserializes single object from an input stream. */ - public static Serializable getObjectFromStream(InputStream is) throws IOException, - ClassNotFoundException { + public static Serializable getObjectFromStream(InputStream is) + throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(is); Object result = ois.readObject(); ois.close(); @@ -322,8 +278,8 @@ public abstract class SerializationTest extends TestCase { * object to be compared * @return object's comparator */ - public static SerializableAssert defineComparator(TestCase test, - Object object) throws Exception { + public static SerializableAssert defineComparator(TestCase test, Object object) + throws Exception { if (test instanceof SerializableAssert) { return (SerializableAssert) test; @@ -360,9 +316,7 @@ public abstract class SerializationTest extends TestCase { * @param object - * to be compared */ - public static void verifyGolden(TestCase test, Object object) - throws Exception { - + public static void verifyGolden(TestCase test, Object object) throws Exception { verifyGolden(test, object, defineComparator(test, object)); } @@ -381,8 +335,8 @@ public abstract class SerializationTest extends TestCase { * @param comparator - * for comparing (de)serialized objects */ - public static void verifyGolden(TestCase test, Object object, - SerializableAssert comparator) throws Exception { + public static void verifyGolden(TestCase test, Object object, SerializableAssert comparator) + throws Exception { Assert.assertNotNull("Null comparator", comparator); @@ -403,8 +357,7 @@ public abstract class SerializationTest extends TestCase { * @param objects - * array of objects to be compared */ - public static void verifyGolden(TestCase test, Object[] objects) - throws Exception { + public static void verifyGolden(TestCase test, Object[] objects) throws Exception { Assert.assertFalse("Empty array", objects.length == 0); verifyGolden(test, objects, defineComparator(test, objects[0])); @@ -428,8 +381,8 @@ public abstract class SerializationTest extends TestCase { * @param comparator - * for comparing (de)serialized objects */ - public static void verifyGolden(TestCase test, Object[] objects, - SerializableAssert comparator) throws Exception { + public static void verifyGolden(TestCase test, Object[] objects, SerializableAssert comparator) + throws Exception { Assert.assertFalse("Empty array", objects.length == 0); for (int i = 0; i < objects.length; i++) { @@ -448,8 +401,7 @@ public abstract class SerializationTest extends TestCase { * @param object - * to be serialized/deserialized */ - public static void verifySelf(Object object) - throws Exception { + public static void verifySelf(Object object) throws Exception { verifySelf(object, defineComparator(null, object)); } @@ -466,8 +418,7 @@ public abstract class SerializationTest extends TestCase { * for comparing serialized/deserialized object with initial * object */ - public static void verifySelf(Object object, SerializableAssert comparator) - throws Exception { + public static void verifySelf(Object object, SerializableAssert comparator) throws Exception { Serializable initial = (Serializable) object; @@ -484,8 +435,7 @@ public abstract class SerializationTest extends TestCase { * @param objects - * array of objects to be serialized/deserialized */ - public static void verifySelf(Object[] objects) - throws Exception { + public static void verifySelf(Object[] objects) throws Exception { Assert.assertFalse("Empty array", objects.length == 0); verifySelf(objects, defineComparator(null, objects[0])); @@ -513,8 +463,7 @@ public abstract class SerializationTest extends TestCase { } } - private static Serializable getObject(TestCase test, String toAppend) - throws Exception { + private static Serializable getObject(TestCase test, String toAppend) throws Exception { StringBuilder path = new StringBuilder("/serialization"); @@ -546,8 +495,8 @@ public abstract class SerializationTest extends TestCase { * @throws IOException - * if I/O error */ - public static void createGoldenFile(String root, TestCase test, - Object object) throws IOException { + public static void createGoldenFile(String root, TestCase test, Object object) + throws IOException { String goldenPath = test.getClass().getName().replace('.', File.separatorChar) diff --git a/support/src/test/java/tests/support/resource/Support_Resources.java b/support/src/test/java/tests/support/resource/Support_Resources.java index c1594d4..80a53fb 100644 --- a/support/src/test/java/tests/support/resource/Support_Resources.java +++ b/support/src/test/java/tests/support/resource/Support_Resources.java @@ -17,16 +17,15 @@ package tests.support.resource; -import libcore.io.Streams; -import tests.support.Support_Configuration; - import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; +import tests.support.Support_Configuration; public class Support_Resources { @@ -118,13 +117,24 @@ public class Support_Resources { public static void copyLocalFileto(File dest, InputStream in) throws IOException { if (!dest.exists()) { FileOutputStream out = new FileOutputStream(dest); - Streams.copy(in, out); + copy(in, out); out.close(); dest.deleteOnExit(); } in.close(); } + private static int copy(InputStream in, OutputStream out) throws IOException { + int total = 0; + byte[] buffer = new byte[8192]; + int c; + while ((c = in.read(buffer)) != -1) { + total += c; + out.write(buffer, 0, c); + } + return total; + } + public static File getExternalLocalFile(String url) throws IOException { File resources = createTempFolder(); InputStream in = new URL(url).openStream(); |