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: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.IOException;
026: import java.security.KeyStore;
027: import java.security.PrivateKey;
028: import java.security.Provider;
029: import java.security.Security;
030: import java.security.cert.Certificate;
031:
032: import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
033: import org.apache.harmony.security.tests.support.SpiEngUtils;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: * Tests for <code>KeyStore</code> constructor and methods
039: *
040: */
041:
042: public class KeyStore_Impl3Test extends TestCase {
043:
044: private static final String KeyStoreProviderClass = "org.apache.harmony.security.tests.support.MyKeyStore";
045:
046: private static final String defaultType = "KeyStore";
047:
048: public static boolean KSSupported = false;
049:
050: public static String defaultProviderName = null;
051:
052: public static Provider defaultProvider = null;
053:
054: private static String NotSupportMsg = "Default KeyStore type is not supported";
055:
056: Provider mProv;
057:
058: public KeyStore[] createKS() throws Exception {
059: assertTrue(NotSupportMsg, KSSupported);
060: KeyStore[] kpg = new KeyStore[3];
061:
062: kpg[0] = KeyStore.getInstance(defaultType);
063: kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
064: kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
065: return kpg;
066: }
067:
068: protected void setUp() throws Exception {
069: super .setUp();
070: mProv = (new SpiEngUtils()).new MyProvider("MyKSProvider",
071: "Testing provider", KeyStoreTestSupport.srvKeyStore
072: .concat(".").concat(defaultType),
073: KeyStoreProviderClass);
074: Security.insertProviderAt(mProv, 2);
075: defaultProvider = SpiEngUtils.isSupport(defaultType,
076: KeyStoreTestSupport.srvKeyStore);
077: KSSupported = (defaultProvider != null);
078: defaultProviderName = (KSSupported ? defaultProvider.getName()
079: : null);
080: }
081:
082: /*
083: * @see TestCase#tearDown()
084: */
085: protected void tearDown() throws Exception {
086: super .tearDown();
087: Security.removeProvider(mProv.getName());
088: }
089:
090: /**
091: * Test for <code>load(InputStream stream, char[] password)</code>
092: * <code>store(InputStream stream, char[] password)</code>
093: * <code>size()</code>
094: * <code>getCreationDate(String alias)</code>
095: * methods
096: * Assertions: store(...) throws NullPointerException when stream or
097: * password is null;
098: * getCreationDate(...) throws NullPointerException when alias is null;
099: * stores KeyStore and then loads it;
100: * @throws Exception
101: */
102:
103: public void testLoadStore01() throws Exception {
104: assertTrue(NotSupportMsg, KSSupported);
105:
106: String tType = "TestType";
107: KeyStore.TrustedCertificateEntry tCert = new KeyStore.TrustedCertificateEntry(
108: new KeyStoreTestSupport.MCertificate("type",
109: new byte[0]));
110:
111: Certificate certs[] = {
112: new KeyStoreTestSupport.MCertificate(tType,
113: new byte[10]),
114: new KeyStoreTestSupport.MCertificate(tType,
115: new byte[20]) };
116: PrivateKey pk = new KeyStoreTestSupport.MyPrivateKey(tType, "",
117: new byte[10]);
118: KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(
119: pk, certs);
120: char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
121: KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(
122: pwd);
123: String[] aliases = { "Alias1", "Alias2", "Alias3", "Alias4",
124: "Alias5" };
125:
126: KeyStore[] kss = createKS();
127: KeyStore[] kss1 = new KeyStore[kss.length];
128: assertNotNull("KeyStore objects were not created", kss);
129:
130: for (int i = 0; i < kss.length; i++) {
131: kss1[i] = kss[i];
132: kss[i].load(null, null);
133: kss[i].setEntry(aliases[0], tCert, null);
134: kss[i].setEntry(aliases[1], pKey, pPath);
135: kss[i].setEntry(aliases[2], pKey, pPath);
136: try {
137: kss[i].getCreationDate(null);
138: fail("NullPointerException should be thrown when alias is null");
139: } catch (NullPointerException e) {
140: }
141:
142: kss[i].setKeyEntry(aliases[3], pk, pwd, certs);
143: kss[i].setCertificateEntry(aliases[4], certs[0]);
144: ByteArrayOutputStream bos = new ByteArrayOutputStream();
145: try {
146: kss[i].store(null, pwd);
147: fail("IOException or NullPointerException should be thrown when stream is null");
148: } catch (IOException e) {
149: } catch (NullPointerException e) {
150: }
151:
152: //RI does not throw exception while password is null.
153: kss[i].store(bos, null);
154:
155: kss[i].store(bos, pwd);
156: ByteArrayInputStream bis = new ByteArrayInputStream(bos
157: .toByteArray());
158: kss1[i].load(bis, pwd);
159: assertEquals("Incorrect size", kss1[i].size(), kss[i]
160: .size());
161: KeyStore.Entry en, en1;
162: for (int j = 0; j < 3; j++) {
163: en = kss[i].getEntry(aliases[j],
164: (j == 0 ? null : pPath));
165: en1 = kss1[i].getEntry(aliases[j], (j == 0 ? null
166: : pPath));
167: if (en instanceof KeyStore.TrustedCertificateEntry) {
168: assertTrue(
169: "Incorrect entry 1",
170: en1 instanceof KeyStore.TrustedCertificateEntry);
171: assertEquals("Incorrect Certificate",
172: ((KeyStore.TrustedCertificateEntry) en)
173: .getTrustedCertificate(),
174: ((KeyStore.TrustedCertificateEntry) en1)
175: .getTrustedCertificate());
176: } else {
177: if (en instanceof KeyStore.PrivateKeyEntry) {
178: assertTrue("Incorrect entry 2",
179: en1 instanceof KeyStore.PrivateKeyEntry);
180: assertEquals("Incorrect Certificate",
181: ((KeyStore.PrivateKeyEntry) en)
182: .getPrivateKey(),
183: ((KeyStore.PrivateKeyEntry) en1)
184: .getPrivateKey());
185: } else {
186: if (en instanceof KeyStore.SecretKeyEntry) {
187: assertTrue(
188: "Incorrect entry 3",
189: en1 instanceof KeyStore.SecretKeyEntry);
190: assertEquals("Incorrect Certificate",
191: ((KeyStore.SecretKeyEntry) en)
192: .getSecretKey(),
193: ((KeyStore.SecretKeyEntry) en1)
194: .getSecretKey());
195: }
196: }
197: }
198:
199: assertEquals("Incorrect date", kss[i]
200: .getCreationDate(aliases[j]), kss1[i]
201: .getCreationDate(aliases[j]));
202: }
203: assertEquals("Incorrect entry", kss[i].getKey(aliases[3],
204: pwd), kss1[i].getKey(aliases[3], pwd));
205: assertEquals("Incorrect date", kss[i]
206: .getCreationDate(aliases[3]), kss1[i]
207: .getCreationDate(aliases[3]));
208: assertEquals("Incorrect entry", kss[i]
209: .getCertificate(aliases[4]), kss1[i]
210: .getCertificate(aliases[4]));
211: assertEquals("Incorrect date", kss[i]
212: .getCreationDate(aliases[4]), kss1[i]
213: .getCreationDate(aliases[4]));
214: }
215: }
216: }
|