001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.tests.java.security;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.security.Key;
025: import java.security.KeyPair;
026: import java.security.KeyPairGenerator;
027: import java.security.KeyStore;
028: import java.security.KeyStoreException;
029: import java.security.KeyStoreSpi;
030: import java.security.NoSuchAlgorithmException;
031: import java.security.Provider;
032: import java.security.UnrecoverableKeyException;
033: import java.security.cert.Certificate;
034: import java.security.cert.CertificateException;
035: import java.security.cert.CertificateFactory;
036: import java.util.Date;
037: import java.util.Enumeration;
038:
039: import junit.framework.TestCase;
040:
041: public class KeyStore3Test extends TestCase {
042:
043: private KeyStore mockKeyStore;
044:
045: private KeyPair keyPair;
046:
047: private Certificate certificate;
048:
049: public KeyStore3Test() throws Exception {
050: KeyPairGenerator keyPairGenerator = KeyPairGenerator
051: .getInstance("DSA");
052: keyPair = keyPairGenerator.generateKeyPair();
053:
054: String certificateData = "-----BEGIN CERTIFICATE-----\n"
055: + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
056: + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
057: + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
058: + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
059: + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
060: + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
061: + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
062: + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
063: + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
064: + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
065: + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
066: + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
067: + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
068: + "-----END CERTIFICATE-----\n";
069:
070: ByteArrayInputStream certArray = new ByteArrayInputStream(
071: certificateData.getBytes());
072: CertificateFactory cf = CertificateFactory.getInstance("X.509");
073: certificate = cf.generateCertificate(certArray);
074: }
075:
076: public void test_load() throws Exception {
077: // No exception should be thrown out.
078: mockKeyStore.load(null);
079: }
080:
081: public void test_store() throws Exception {
082: try {
083: mockKeyStore.store(null);
084: fail("should throw KeyStoreException: not initialized");
085: } catch (KeyStoreException e) {
086: // expected
087: }
088:
089: // No exception should be thrown out.
090: mockKeyStore.load(null, null);
091: mockKeyStore.store(null);
092: }
093:
094: public void test_setKeyEntry_null() throws Exception {
095: mockKeyStore.load(null, null);
096: // No exception should be thrown out.
097: mockKeyStore.setKeyEntry(null, null, null, null);
098: }
099:
100: public void test_setKeyEntry_key_is_null() throws Exception {
101: mockKeyStore.load(null, null);
102: // No exception should be thrown out.
103: mockKeyStore.setKeyEntry("Alias", null, null,
104: new Certificate[] { certificate });
105: }
106:
107: public void test_setKeyEntry_key_is_private() throws Exception {
108: mockKeyStore.load(null, null);
109: Key key = keyPair.getPrivate();
110: try {
111: mockKeyStore.setKeyEntry("Alias", key, null, null);
112: fail("should throw IllegalArgumentException");
113: } catch (IllegalArgumentException e) {
114: // expected
115: }
116:
117: try {
118: mockKeyStore.setKeyEntry("Alias", key, null,
119: new Certificate[0]);
120: fail("should throw IllegalArgumentException");
121: } catch (IllegalArgumentException e) {
122: // expected
123: }
124:
125: mockKeyStore.setKeyEntry("Alias", key, null,
126: new Certificate[] { certificate });
127: }
128:
129: public void test_setKeyEntry_key_is_public() throws Exception {
130: mockKeyStore.load(null, null);
131: Key key = keyPair.getPublic();
132: mockKeyStore.setKeyEntry("Alias1", key, null, null);
133: mockKeyStore.setKeyEntry("Alias2", key, null,
134: new Certificate[0]);
135: mockKeyStore.setKeyEntry("Alias3", key, null,
136: new Certificate[] { certificate });
137: }
138:
139: public void test_setCertificateEntry_null() throws Exception {
140: mockKeyStore.load(null, null);
141:
142: mockKeyStore.setCertificateEntry(null, null);
143:
144: mockKeyStore.setCertificateEntry(null, certificate);
145:
146: mockKeyStore.setCertificateEntry("Alias", null);
147: }
148:
149: public void test_store_null() throws Exception {
150: mockKeyStore.load(null, null);
151: mockKeyStore.store(null, null);
152: }
153:
154: protected void setUp() throws Exception {
155: super .setUp();
156: mockKeyStore = new MyKeyStore(new MyKeyStoreSpi(), null,
157: "MyKeyStore");
158: }
159:
160: private static class MyKeyStore extends KeyStore {
161:
162: public MyKeyStore(KeyStoreSpi keyStoreSpi, Provider provider,
163: String type) {
164: super (keyStoreSpi, provider, type);
165: }
166: }
167:
168: private static class MyKeyStoreSpi extends KeyStoreSpi {
169:
170: public Enumeration<String> engineAliases() {
171: return null;
172: }
173:
174: public boolean engineContainsAlias(String arg0) {
175: return false;
176: }
177:
178: public void engineDeleteEntry(String arg0)
179: throws KeyStoreException {
180: }
181:
182: public Certificate engineGetCertificate(String arg0) {
183: return null;
184: }
185:
186: public String engineGetCertificateAlias(Certificate arg0) {
187: return null;
188: }
189:
190: public Certificate[] engineGetCertificateChain(String arg0) {
191: return null;
192: }
193:
194: public Date engineGetCreationDate(String arg0) {
195: return null;
196: }
197:
198: public Key engineGetKey(String arg0, char[] arg1)
199: throws NoSuchAlgorithmException,
200: UnrecoverableKeyException {
201: return null;
202: }
203:
204: public boolean engineIsCertificateEntry(String arg0) {
205: return false;
206: }
207:
208: public boolean engineIsKeyEntry(String arg0) {
209: return false;
210: }
211:
212: public void engineLoad(InputStream arg0, char[] arg1)
213: throws IOException, NoSuchAlgorithmException,
214: CertificateException {
215: return;
216: }
217:
218: public void engineSetCertificateEntry(String arg0,
219: Certificate arg1) throws KeyStoreException {
220: return;
221: }
222:
223: public void engineSetKeyEntry(String arg0, byte[] arg1,
224: Certificate[] arg2) throws KeyStoreException {
225: return;
226: }
227:
228: public void engineSetKeyEntry(String arg0, Key arg1,
229: char[] arg2, Certificate[] arg3)
230: throws KeyStoreException {
231: return;
232: }
233:
234: public int engineSize() {
235: return 0;
236: }
237:
238: public void engineStore(KeyStore.LoadStoreParameter param) {
239: return;
240: }
241:
242: public void engineStore(OutputStream arg0, char[] arg1)
243: throws IOException, NoSuchAlgorithmException,
244: CertificateException {
245: return;
246: }
247: }
248:
249: }
|