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.IOException;
024: import java.io.InputStream;
025: import java.security.KeyStore;
026: import java.security.KeyStoreException;
027: import java.security.KeyStoreSpi;
028: import java.security.NoSuchAlgorithmException;
029: import java.security.UnrecoverableEntryException;
030: import java.security.UnrecoverableKeyException;
031: import java.security.cert.Certificate;
032: import java.security.cert.CertificateException;
033: import java.util.Date;
034:
035: import junit.framework.TestCase;
036:
037: import org.apache.harmony.security.tests.support.MyKeyStoreSpi;
038: import org.apache.harmony.security.tests.support.MyLoadStoreParams;
039: import org.apache.harmony.security.tests.support.cert.MyCertificate;
040:
041: /**
042: * Tests for <code>KeyStoreSpi</code> constructor and methods
043: *
044: */
045:
046: public class KeyStoreSpiTest extends TestCase {
047:
048: /**
049: * Constructor for KeyStoreSpi.
050: *
051: * @param arg0
052: */
053: public KeyStoreSpiTest(String arg0) {
054: super (arg0);
055: }
056:
057: /**
058: * Test for <code>KeyStoreSpi()</code> constructor
059: * and the following methods:
060: * <code>engineLoad(KeyStore.LoadStoreParameter param)</code>
061: * <code>engineStore(KeyStore.LoadStoreParameter param)</code>
062: * <code>engineGetEntry(String alias, KeyStore.ProtectionParameter param)</code>
063: * <code>engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter param)</code>
064: * Assertions:
065: * creates new KeyStoreSpi object;
066: * engineGetEntry(..) returns null entry;
067: * engineStore(..) throws UnexpectedOperationException;
068: * engineSetEntry(..) throws KeyStoreException or NullPointerException
069: */
070: public void testKeyStoteSpi01() throws IOException,
071: NoSuchAlgorithmException, CertificateException,
072: UnrecoverableEntryException, KeyStoreException {
073: KeyStoreSpi ksSpi = new MyKeyStoreSpi();
074:
075: tmpEntry entry = new tmpEntry();
076: tmpProtection pPar = new tmpProtection();
077:
078: try {
079: ksSpi.engineStore(null);
080: } catch (UnsupportedOperationException e) {
081: }
082: assertNull("Not null entry", ksSpi.engineGetEntry("aaa", null));
083: assertNull("Not null entry", ksSpi.engineGetEntry(null, pPar));
084: assertNull("Not null entry", ksSpi.engineGetEntry("aaa", pPar));
085:
086: try {
087: ksSpi.engineSetEntry("", null, null);
088: fail("KeyStoreException or NullPointerException must be thrown");
089: } catch (KeyStoreException e) {
090: } catch (NullPointerException e) {
091: }
092:
093: try {
094: ksSpi.engineSetEntry("",
095: new KeyStore.TrustedCertificateEntry(
096: new MyCertificate("type", new byte[0])),
097: null);
098: fail("KeyStoreException must be thrown");
099: } catch (KeyStoreException e) {
100: }
101:
102: try {
103: ksSpi.engineSetEntry("aaa", entry, null);
104: fail("KeyStoreException must be thrown");
105: } catch (KeyStoreException e) {
106: }
107: }
108:
109: /**
110: * Test for <code>KeyStoreSpi()</code> constructor
111: * and abstract engine methods.
112: * Assertion: creates new KeyStoreSpi object.
113: */
114: public void testKeyStoteSpi02() throws NoSuchAlgorithmException,
115: UnrecoverableKeyException, CertificateException {
116: KeyStoreSpi ksSpi = new MyKeyStoreSpi();
117: assertNull("engineGetKey(..) must return null", ksSpi
118: .engineGetKey("", new char[0]));
119: assertNull("engineGetCertificateChain(..) must return null",
120: ksSpi.engineGetCertificateChain(""));
121: assertNull("engineGetCertificate(..) must return null", ksSpi
122: .engineGetCertificate(""));
123: assertEquals("engineGetCreationDate(..) must return Date(0)",
124: new Date(0), ksSpi.engineGetCreationDate(""));
125: try {
126: ksSpi.engineSetKeyEntry("", null, new char[0],
127: new Certificate[0]);
128: fail("KeyStoreException must be thrown from engineSetKeyEntry(..)");
129: } catch (KeyStoreException e) {
130: }
131: try {
132: ksSpi
133: .engineSetKeyEntry("", new byte[0],
134: new Certificate[0]);
135: fail("KeyStoreException must be thrown from engineSetKeyEntry(..)");
136: } catch (KeyStoreException e) {
137: }
138: try {
139: ksSpi.engineSetCertificateEntry("", null);
140: fail("KeyStoreException must be thrown from engineSetCertificateEntry(..)");
141: } catch (KeyStoreException e) {
142: }
143: try {
144: ksSpi.engineDeleteEntry("");
145: fail("KeyStoreException must be thrown from engineDeleteEntry(..)");
146: } catch (KeyStoreException e) {
147: }
148: assertNull("engineAliases() must return null", ksSpi
149: .engineAliases());
150: assertFalse("engineContainsAlias(..) must return false", ksSpi
151: .engineContainsAlias(""));
152: assertEquals("engineSize() must return 0", 0, ksSpi
153: .engineSize());
154: try {
155: ksSpi.engineStore(null, null);
156: fail("IOException must be thrown");
157: } catch (IOException e) {
158: }
159: }
160:
161: /**
162: * @tests java.security.KeyStoreSpi#engineLoad(KeyStore.LoadStoreParameter)
163: */
164: public void test_engineLoadLjava_security_KeyStore_LoadStoreParameter()
165: throws Exception {
166:
167: final String msg = "error";
168:
169: KeyStoreSpi ksSpi = new MyKeyStoreSpi() {
170: public void engineLoad(InputStream stream, char[] password) {
171: assertNull(stream);
172: assertNull(password);
173: throw new RuntimeException(msg);
174: }
175: };
176: try {
177: ksSpi.engineLoad(null);
178: fail("Should throw exception");
179: } catch (RuntimeException e) {
180: assertSame(msg, e.getMessage());
181: }
182:
183: // test: protection parameter is null
184: try {
185: ksSpi.engineLoad(new MyLoadStoreParams(null));
186: fail("No expected UnsupportedOperationException");
187: } catch (UnsupportedOperationException e) {
188: }
189:
190: // test: protection parameter is not instanceof
191: // PasswordProtection or CallbackHandlerProtection
192: try {
193: ksSpi
194: .engineLoad(new MyLoadStoreParams(
195: new tmpProtection()));
196: fail("No expected UnsupportedOperationException");
197: } catch (UnsupportedOperationException e) {
198: }
199: }
200:
201: public static void main(String args[]) {
202: junit.textui.TestRunner.run(KeyStoreSpiTest.class);
203: }
204: }
205:
206: /**
207: * Additional class implements KeyStore.Entry interface
208: */
209: class tmpEntry implements KeyStore.Entry {
210: }
211:
212: class tmpProtection implements KeyStore.ProtectionParameter {
213: }
|