You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2007/04/06 05:42:14 UTC

svn commit: r526041 - in /harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/java/security/ test/api/java/org/apache/harmony/security/tests/java/security/ test/impl/java/org/apache/harmony/security/tests/java/security/

Author: pyang
Date: Thu Apr  5 20:42:13 2007
New Revision: 526041

URL: http://svn.apache.org/viewvc?view=rev&rev=526041
Log:
Apply patch for HARMONY-3549([classlib][security]KeyStore.load(null) should not throw exception on the level of KeyStore class.)

Added:
    harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStore3Test.java
Modified:
    harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStore.java
    harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/Signature2Test.java
    harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl1Test.java
    harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl2Test.java

Modified: harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStore.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStore.java?view=diff&rev=526041&r1=526040&r2=526041
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStore.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStore.java Thu Apr  5 20:42:13 2007
@@ -236,16 +236,12 @@
         if (!isInit) {
             throw new KeyStoreException(NOTINITKEYSTORE);
         }
-        if (alias == null) {
-            throw new NullPointerException(Messages.getString("security.3F")); //$NON-NLS-1$
-        }
-        if (key == null) {
-            throw new KeyStoreException(Messages.getString("security.2C")); //$NON-NLS-1$
-        }
+
         // Certificate chain is required for PrivateKey
-        if ((key instanceof PrivateKey)
-                && ((chain == null) || chain.length == 0)) {
-            throw new KeyStoreException(Messages.getString("security.52")); //$NON-NLS-1$
+        if (null != key && key instanceof PrivateKey
+                && (chain == null || chain.length == 0)) {
+            throw new IllegalArgumentException(Messages
+                    .getString("security.52")); //$NON-NLS-1$
         }
         implSpi.engineSetKeyEntry(alias, key, password, chain);
     }
@@ -273,9 +269,6 @@
         if (!isInit) {
             throw new KeyStoreException(NOTINITKEYSTORE);
         }
-        if (alias == null) {
-            throw new NullPointerException(Messages.getString("security.3F")); //$NON-NLS-1$
-        }
         implSpi.engineSetCertificateEntry(alias, cert);
     }
 
@@ -405,9 +398,6 @@
         if (!isInit) {
             throw new KeyStoreException(NOTINITKEYSTORE);
         }
-        if (param == null) {
-            throw new IOException(Messages.getString("security.3E")); //$NON-NLS-1$
-        }
         implSpi.engineStore(param);
     }
 
@@ -427,9 +417,6 @@
      */
     public final void load(LoadStoreParameter param) throws IOException,
             NoSuchAlgorithmException, CertificateException {
-        if (param == null) {
-            throw new IOException(Messages.getString("security.3E")); //$NON-NLS-1$
-        }
         implSpi.engineLoad(param);
         isInit = true;
     }

Added: harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStore3Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStore3Test.java?view=auto&rev=526041
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStore3Test.java (added)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStore3Test.java Thu Apr  5 20:42:13 2007
@@ -0,0 +1,238 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.security.tests.java.security;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.Key;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.KeyStoreSpi;
+import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.util.Date;
+import java.util.Enumeration;
+
+import junit.framework.TestCase;
+
+public class KeyStore3Test extends TestCase {
+    
+    private KeyStore mockKeyStore;
+    
+    private KeyPair keyPair;
+    
+    private Certificate certificate;
+    
+    public KeyStore3Test() throws Exception {
+        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
+        keyPair = keyPairGenerator.generateKeyPair();
+
+        String certificateData = "-----BEGIN CERTIFICATE-----\n"
+                + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
+                + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
+                + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
+                + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
+                + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
+                + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
+                + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
+                + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
+                + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
+                + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
+                + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
+                + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
+                + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
+                + "-----END CERTIFICATE-----\n";
+
+        ByteArrayInputStream certArray = new ByteArrayInputStream(
+                certificateData.getBytes());
+        CertificateFactory cf = CertificateFactory.getInstance("X.509");
+        certificate = cf.generateCertificate(certArray);
+    }
+    
+    public void test_load() throws Exception {
+        // No exception should be thrown out.
+        mockKeyStore.load(null);
+    }
+
+    public void test_store() throws Exception {
+        try {
+            mockKeyStore.store(null);
+            fail("should throw KeyStoreException: not initialized");
+        } catch (KeyStoreException e) {
+            // expected
+        }
+        
+        // No exception should be thrown out.
+        mockKeyStore.load(null, null);
+        mockKeyStore.store(null);
+    }
+
+    public void test_setKeyEntry_null() throws Exception {
+        mockKeyStore.load(null, null); 
+        // No exception should be thrown out.
+        mockKeyStore.setKeyEntry(null, null, null, null);        
+    }
+    
+    public void test_setKeyEntry_key_is_null() throws Exception {
+        mockKeyStore.load(null, null);
+        // No exception should be thrown out.
+        mockKeyStore.setKeyEntry("Alias", null, null, new Certificate[]{certificate});        
+    }
+    
+    public void test_setKeyEntry_key_is_private() throws Exception {
+        mockKeyStore.load(null, null);
+        Key key = keyPair.getPrivate();
+        try {
+            mockKeyStore.setKeyEntry("Alias", key, null, null);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        
+        try {
+            mockKeyStore.setKeyEntry("Alias", key, null,
+                    new Certificate[0]);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        
+        mockKeyStore.setKeyEntry("Alias", key, null, new Certificate[]{certificate});        
+    }
+    
+    public void test_setKeyEntry_key_is_public() throws Exception
+    {
+        mockKeyStore.load(null, null);
+        Key key = keyPair.getPublic();
+        mockKeyStore.setKeyEntry("Alias1", key, null, null);
+        mockKeyStore.setKeyEntry("Alias2", key, null,
+                new Certificate[0]);
+        mockKeyStore.setKeyEntry("Alias3", key, null, new Certificate[]{certificate});
+    }
+    
+    public void test_setCertificateEntry_null() throws Exception {
+        mockKeyStore.load(null, null);
+
+        mockKeyStore.setCertificateEntry(null, null);
+
+        mockKeyStore.setCertificateEntry(null, certificate);
+
+        mockKeyStore.setCertificateEntry("Alias", null);
+    }
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        mockKeyStore = new MyKeyStore(new MyKeyStoreSpi(), null, "MyKeyStore");
+    }
+    
+    private static class MyKeyStore extends KeyStore {
+
+        public MyKeyStore(KeyStoreSpi keyStoreSpi, Provider provider,
+                String type) {
+            super(keyStoreSpi, provider, type);
+        }
+    }
+
+    private static class MyKeyStoreSpi extends KeyStoreSpi {
+
+        public Enumeration<String> engineAliases() {
+            return null;
+        }
+
+        public boolean engineContainsAlias(String arg0) {
+            return false;
+        }
+
+        public void engineDeleteEntry(String arg0) throws KeyStoreException {
+        }
+
+        public Certificate engineGetCertificate(String arg0) {
+            return null;
+        }
+
+        public String engineGetCertificateAlias(Certificate arg0) {
+            return null;
+        }
+
+        public Certificate[] engineGetCertificateChain(String arg0) {
+            return null;
+        }
+
+        public Date engineGetCreationDate(String arg0) {
+            return null;
+        }
+
+        public Key engineGetKey(String arg0, char[] arg1)
+                throws NoSuchAlgorithmException, UnrecoverableKeyException {
+            return null;
+        }
+
+        public boolean engineIsCertificateEntry(String arg0) {
+            return false;
+        }
+
+        public boolean engineIsKeyEntry(String arg0) {
+            return false;
+        }
+
+        public void engineLoad(InputStream arg0, char[] arg1)
+                throws IOException, NoSuchAlgorithmException,
+                CertificateException {
+            return;
+        }
+
+        public void engineSetCertificateEntry(String arg0, Certificate arg1)
+                throws KeyStoreException {
+            return;
+        }
+
+        public void engineSetKeyEntry(String arg0, byte[] arg1,
+                Certificate[] arg2) throws KeyStoreException {
+            return;
+        }
+
+        public void engineSetKeyEntry(String arg0, Key arg1, char[] arg2,
+                Certificate[] arg3) throws KeyStoreException {
+            return;
+        }
+
+        public int engineSize() {
+            return 0;
+        }
+
+        public void engineStore(KeyStore.LoadStoreParameter param){
+            return;
+        }
+
+        public void engineStore(OutputStream arg0, char[] arg1)
+                throws IOException, NoSuchAlgorithmException,
+                CertificateException {
+            return;
+        }        
+    }    
+
+}
+

Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/Signature2Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/Signature2Test.java?view=diff&rev=526041&r1=526040&r2=526041
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/Signature2Test.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/Signature2Test.java Thu Apr  5 20:42:13 2007
@@ -80,6 +80,18 @@
        			Signature.getInstance("DSA", providers[i].getName());
        		}// end for
 	}
+    
+    /**
+     * @tests java.security.Signature#getParameters()
+     */
+    public void test_getParameters() throws Exception {
+        Signature sig = Signature.getInstance("DSA");
+        try {
+            sig.getParameters();
+        } catch (UnsupportedOperationException e) {
+            // Could be that the operation is not supported
+        }
+    }
 
 	/**
 	 * @tests java.security.Signature#getParameter(java.lang.String)

Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl1Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl1Test.java?view=diff&rev=526041&r1=526040&r2=526041
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl1Test.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl1Test.java Thu Apr  5 20:42:13 2007
@@ -740,11 +740,9 @@
         assertNotNull("KeyStore objects were not created", kss);
         for (int i = 0; i < kss.length; i++) {
             kss[i].load(null, null);
-            try {
-                kss[i].setKeyEntry(null, key, pwd, certs);
-                fail("NullPointerException should be thrown when alias is null");
-            } catch (NullPointerException e) {
-            }
+            
+            // Null as alias does not necessarily lead to NullPointerException
+            
             try {
                 kss[i].setKeyEntry("ZZZ", null, pwd, certs);
                 fail("KeyStoreException should be thrown when key is null");
@@ -754,14 +752,14 @@
                 kss[i].setKeyEntry("ZZZ", key, pwd, null);
                 fail("KeyStoreException or IllegalArgumentException should be thrown "
                         + "when chain is null and key is private");
-            } catch (KeyStoreException e) {
+            } catch (IllegalArgumentException e) {
             }
             try {
                 kss[i].setKeyEntry("ZZZ", key, pwd,
                         new KeyStoreTestSupport.MCertificate[0]);
                 fail("KeyStoreException or IllegalArgumentException should be thrown "
                         + "when chain is empty and key is private");
-            } catch (KeyStoreException e) {
+            } catch (IllegalArgumentException e) {
             }
 
             for (int j = 0; j < aliases.length; j++)  {

Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl2Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl2Test.java?view=diff&rev=526041&r1=526040&r2=526041
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl2Test.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl2Test.java Thu Apr  5 20:42:13 2007
@@ -157,15 +157,13 @@
                 new KeyStore.PasswordProtection(new char[0]));        
         try {
             keyS.store(null);
-            fail("IOException must be thrown when param is null");
-        } catch (IOException e) {
+            fail("UnsupportedOperationException must be thrown");
+        } catch (UnsupportedOperationException e) {
         }
 
-        try {
-            keyS.load(null);
-            fail("load(null) does not throw any exception");
-        } catch (IOException e) {
-        }
+        
+        //No exception should be thrown out.
+        keyS.load(null);
 
         try {
             keyS.store(lParam);