Home | History | Annotate | Download | only in security
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.security.tests.java.security;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import org.apache.harmony.security.tests.java.security.AlgorithmParametersTest.MyAlgorithmParameters;
     23 
     24 import java.io.ByteArrayInputStream;
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 import java.io.OutputStream;
     28 import java.security.Key;
     29 import java.security.KeyPair;
     30 import java.security.KeyPairGenerator;
     31 import java.security.KeyStore;
     32 import java.security.KeyStoreException;
     33 import java.security.KeyStoreSpi;
     34 import java.security.NoSuchAlgorithmException;
     35 import java.security.Provider;
     36 import java.security.UnrecoverableKeyException;
     37 import java.security.cert.Certificate;
     38 import java.security.cert.CertificateException;
     39 import java.security.cert.CertificateFactory;
     40 import java.util.Date;
     41 import java.util.Enumeration;
     42 
     43 public class KeyStore3Test extends TestCase {
     44 
     45     private KeyStore mockKeyStore;
     46 
     47     private KeyPair keyPair;
     48 
     49     private Certificate certificate;
     50 
     51     public void test_load() throws Exception {
     52         // No exception should be thrown out.
     53         mockKeyStore.load(null);
     54     }
     55 
     56     public void test_store() throws Exception {
     57         try {
     58             mockKeyStore.store(null);
     59             fail("should throw KeyStoreException: not initialized");
     60         } catch (KeyStoreException e) {
     61             // expected
     62         }
     63 
     64         // No exception should be thrown out.
     65         mockKeyStore.load(null, null);
     66         mockKeyStore.store(null);
     67     }
     68 
     69     public void test_setKeyEntry_null() throws Exception {
     70         mockKeyStore.load(null, null);
     71         // No exception should be thrown out.
     72         mockKeyStore.setKeyEntry(null, null, null, null);
     73     }
     74 
     75     public void test_setKeyEntry_key_is_null() throws Exception {
     76         mockKeyStore.load(null, null);
     77         // No exception should be thrown out.
     78         mockKeyStore.setKeyEntry("Alias", null, null, new Certificate[]{certificate});
     79     }
     80 
     81     public void test_setKeyEntry_key_is_private() throws Exception {
     82         mockKeyStore.load(null, null);
     83         Key key = keyPair.getPrivate();
     84         try {
     85             mockKeyStore.setKeyEntry("Alias", key, null, null);
     86             fail("should throw IllegalArgumentException");
     87         } catch (IllegalArgumentException e) {
     88             // expected
     89         }
     90 
     91         try {
     92             mockKeyStore.setKeyEntry("Alias", key, null,
     93                     new Certificate[0]);
     94             fail("should throw IllegalArgumentException");
     95         } catch (IllegalArgumentException e) {
     96             // expected
     97         }
     98 
     99         mockKeyStore.setKeyEntry("Alias", key, null, new Certificate[]{certificate});
    100     }
    101 
    102     public void test_setKeyEntry_key_is_public() throws Exception
    103     {
    104         mockKeyStore.load(null, null);
    105         Key key = keyPair.getPublic();
    106         mockKeyStore.setKeyEntry("Alias1", key, null, null);
    107         mockKeyStore.setKeyEntry("Alias2", key, null,
    108                 new Certificate[0]);
    109         mockKeyStore.setKeyEntry("Alias3", key, null, new Certificate[]{certificate});
    110     }
    111 
    112     public void test_setCertificateEntry_null() throws Exception {
    113         mockKeyStore.load(null, null);
    114 
    115         mockKeyStore.setCertificateEntry(null, null);
    116 
    117         mockKeyStore.setCertificateEntry(null, certificate);
    118 
    119         mockKeyStore.setCertificateEntry("Alias", null);
    120     }
    121 
    122     @SuppressWarnings("cast")
    123     public void test_KeyStore() {
    124         Provider p = new MyProvider();
    125         try {
    126             MyKeyStore ks = new MyKeyStore(new MyKeyStoreSpi(), p, "MyKeyStore");
    127             assertNotNull(ks);
    128             assertTrue(ks instanceof KeyStore);
    129         } catch (Exception e) {
    130             fail("Exception should be not thrown");
    131         }
    132 
    133         try {
    134             MyKeyStore ks = new MyKeyStore(null, null, null);
    135             assertNotNull(ks);
    136             assertTrue(ks instanceof KeyStore);
    137         } catch (Exception e) {
    138             fail("Exception should be not thrown");
    139         }
    140 
    141     }
    142 
    143     protected void setUp() throws Exception {
    144         super.setUp();
    145 
    146         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    147         keyPair = keyPairGenerator.generateKeyPair();
    148 
    149         String certificateData = "-----BEGIN CERTIFICATE-----\n"
    150                 + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
    151                 + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
    152                 + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
    153                 + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
    154                 + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
    155                 + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
    156                 + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
    157                 + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
    158                 + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
    159                 + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
    160                 + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
    161                 + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
    162                 + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
    163                 + "-----END CERTIFICATE-----\n";
    164         ByteArrayInputStream certArray = new ByteArrayInputStream(
    165                 certificateData.getBytes());
    166         CertificateFactory cf = CertificateFactory.getInstance("X.509");
    167         certificate = cf.generateCertificate(certArray);
    168         mockKeyStore = new MyKeyStore(new MyKeyStoreSpi(), null, "MyKeyStore");
    169     }
    170 
    171     private static class MyKeyStore extends KeyStore {
    172 
    173         public MyKeyStore(KeyStoreSpi keyStoreSpi, Provider provider,
    174                 String type) {
    175             super(keyStoreSpi, provider, type);
    176         }
    177     }
    178 
    179     @SuppressWarnings("unused")
    180     private static class MyKeyStoreSpi extends KeyStoreSpi {
    181 
    182         public Enumeration<String> engineAliases() {
    183             return null;
    184         }
    185 
    186         public boolean engineContainsAlias(String arg0) {
    187             return false;
    188         }
    189 
    190         public void engineDeleteEntry(String arg0) throws KeyStoreException {
    191         }
    192 
    193         public Certificate engineGetCertificate(String arg0) {
    194             return null;
    195         }
    196 
    197         public String engineGetCertificateAlias(Certificate arg0) {
    198             return null;
    199         }
    200 
    201         public Certificate[] engineGetCertificateChain(String arg0) {
    202             return null;
    203         }
    204 
    205         public Date engineGetCreationDate(String arg0) {
    206             return null;
    207         }
    208 
    209         public Key engineGetKey(String arg0, char[] arg1)
    210                 throws NoSuchAlgorithmException, UnrecoverableKeyException {
    211             return null;
    212         }
    213 
    214         public boolean engineIsCertificateEntry(String arg0) {
    215             return false;
    216         }
    217 
    218         public boolean engineIsKeyEntry(String arg0) {
    219             return false;
    220         }
    221 
    222         public void engineLoad(InputStream arg0, char[] arg1)
    223                 throws IOException, NoSuchAlgorithmException,
    224                 CertificateException {
    225             return;
    226         }
    227 
    228         public void engineSetCertificateEntry(String arg0, Certificate arg1)
    229                 throws KeyStoreException {
    230             return;
    231         }
    232 
    233         public void engineSetKeyEntry(String arg0, byte[] arg1,
    234                 Certificate[] arg2) throws KeyStoreException {
    235             return;
    236         }
    237 
    238         public void engineSetKeyEntry(String arg0, Key arg1, char[] arg2,
    239                 Certificate[] arg3) throws KeyStoreException {
    240             return;
    241         }
    242 
    243         public int engineSize() {
    244             return 0;
    245         }
    246 
    247         public void engineStore(KeyStore.LoadStoreParameter param){
    248             return;
    249         }
    250 
    251         public void engineStore(OutputStream arg0, char[] arg1)
    252                 throws IOException, NoSuchAlgorithmException,
    253                 CertificateException {
    254             return;
    255         }
    256     }
    257 
    258     @SuppressWarnings("serial")
    259     private class MyProvider extends Provider {
    260         MyProvider() {
    261             super("MyProvider", 1.0, "Provider for testing");
    262             put("AlgorithmParameters.ABC", MyAlgorithmParameters.class
    263                     .getName());
    264         }
    265 
    266         MyProvider(String name, double version, String info) {
    267             super(name, version, info);
    268         }
    269     }
    270 
    271 }
    272