0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: /**
0019: * @author Vera Y. Petrashkova
0020: * @version $Revision$
0021: */package org.apache.harmony.security.tests.java.security;
0022:
0023: import java.io.ByteArrayOutputStream;
0024: import java.security.Key;
0025: import java.security.KeyStore;
0026: import java.security.KeyStoreException;
0027: import java.security.KeyStoreSpi;
0028: import java.security.NoSuchProviderException;
0029: import java.security.PrivateKey;
0030: import java.security.Provider;
0031: import java.security.UnrecoverableEntryException;
0032: import java.security.UnrecoverableKeyException;
0033: import java.security.cert.Certificate;
0034: import java.util.Arrays;
0035:
0036: import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
0037: import org.apache.harmony.security.tests.support.MyKeyStoreSpi;
0038: import org.apache.harmony.security.tests.support.MyLoadStoreParams;
0039: import org.apache.harmony.security.tests.support.SpiEngUtils;
0040: import org.apache.harmony.security.tests.support.TestKeyPair;
0041:
0042: import junit.framework.TestCase;
0043:
0044: /**
0045: * Tests for <code>KeyStore</code> constructor and methods
0046: *
0047: */
0048:
0049: public class KeyStore_Impl1Test extends TestCase {
0050:
0051: public static final String srvKeyStore = KeyStoreTestSupport.srvKeyStore;
0052: public static String[] validValues = KeyStoreTestSupport.validValues;
0053:
0054: private static final String[] aliases = { "", "alias", "Alias",
0055: "ALIAS", "new alias", "another alias", "ADDITIONAL",
0056: "THE SAME ALIAS" };
0057:
0058: private static String[] invalidValues = SpiEngUtils.invalidValues;
0059:
0060: public static String defaultType = KeyStoreTestSupport.defaultType;
0061: public static boolean JKSSupported = KeyStoreTestSupport.JKSSupported;
0062: public static String defaultProviderName = KeyStoreTestSupport.defaultProviderName;
0063: public static Provider defaultProvider = KeyStoreTestSupport.defaultProvider;
0064:
0065: private static String NotSupportMsg = "Default KeyStore type is not supported";
0066:
0067: public KeyStore[] createKS() throws Exception {
0068: assertTrue(NotSupportMsg, JKSSupported);
0069: KeyStore[] kpg = new KeyStore[3];
0070:
0071: kpg[0] = KeyStore.getInstance(defaultType);
0072: kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
0073: kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
0074: return kpg;
0075: }
0076:
0077: /**
0078: * Test for <code>getInstance(String type)</code> method
0079: * Assertion:
0080: * returns KeyStoreException object
0081: */
0082: public void testKeyStore03() throws KeyStoreException {
0083: assertTrue(NotSupportMsg, JKSSupported);
0084: KeyStore ks;
0085: for (int i = 0; i < validValues.length; i++) {
0086: ks = KeyStore.getInstance(validValues[i]);
0087: assertEquals("Incorrect type", ks.getType(), validValues[i]);
0088: }
0089: }
0090:
0091: /**
0092: * Test for <code>getInstance(String type, String provider)</code> method
0093: * Assertion: throws IllegalArgumentException when provider is null or empty
0094: */
0095: public void testKeyStore04() throws Exception {
0096: assertTrue(NotSupportMsg, JKSSupported);
0097: String provider = null;
0098: for (int i = 0; i < validValues.length; i++) {
0099: try {
0100: KeyStore.getInstance(validValues[i], provider);
0101: fail("IllegalArgumentException must be thrown when provider is null (type: "
0102: .concat(validValues[i]).concat(" )"));
0103: } catch (IllegalArgumentException e) {
0104: }
0105: try {
0106: KeyStore.getInstance(validValues[i], "");
0107: fail("IllegalArgumentException must be thrown when provider is empty (type: "
0108: .concat(validValues[i]).concat(" )"));
0109: } catch (IllegalArgumentException e) {
0110: }
0111: }
0112: }
0113:
0114: /**
0115: * Test for <code>getInstance(String type, String provider)</code> method
0116: * Assertion: throws NoSuchProviderException when provider is not available
0117: */
0118: public void testKeyStore05() throws KeyStoreException {
0119: assertTrue(NotSupportMsg, JKSSupported);
0120: for (int i = 0; i < validValues.length; i++) {
0121: for (int j = 1; j < invalidValues.length; j++) {
0122: try {
0123: KeyStore.getInstance(validValues[i],
0124: invalidValues[j]);
0125: fail("NoSuchProviderException must be thrown (type: "
0126: .concat(validValues[i]).concat(
0127: " provider: ").concat(
0128: invalidValues[j]).concat(" )"));
0129: } catch (NoSuchProviderException e) {
0130: }
0131: }
0132: }
0133: }
0134:
0135: /**
0136: * Test for <code>getInstance(String type, String provider)</code> method
0137: * Assertion:
0138: * throws NullPointerException when type is null
0139: * throws KeyStoreException when type is not available
0140: */
0141: public void testKeyStore06() throws NoSuchProviderException {
0142: assertTrue(NotSupportMsg, JKSSupported);
0143: try {
0144: KeyStore.getInstance(null, defaultProviderName);
0145: fail("KeyStoreException must be thrown when type is null");
0146: } catch (KeyStoreException e) {
0147: } catch (NullPointerException e) {
0148: }
0149: for (int i = 0; i < invalidValues.length; i++) {
0150: try {
0151: KeyStore.getInstance(invalidValues[i],
0152: defaultProviderName);
0153: fail("KeyStoreException must be thrown (type: ".concat(
0154: invalidValues[i]).concat(" provider: ")
0155: .concat(defaultProviderName).concat(" )"));
0156: } catch (KeyStoreException e) {
0157: }
0158: }
0159: }
0160:
0161: /**
0162: * Test for <code>getInstance(String type, String provider)</code> method
0163: * Assertion: returns KeyStore object
0164: */
0165: public void testKeyStore07() throws Exception {
0166: assertTrue(NotSupportMsg, JKSSupported);
0167: KeyStore ks;
0168: for (int i = 0; i < validValues.length; i++) {
0169: ks = KeyStore.getInstance(validValues[i],
0170: defaultProviderName);
0171: assertEquals("Incorrect type", ks.getType(), validValues[i]);
0172: assertEquals("Incorrect provider", ks.getProvider()
0173: .getName(), defaultProviderName);
0174: }
0175: }
0176:
0177: /**
0178: * Test for <code>getInstance(String type, Provider provider)</code> method
0179: * Assertion: throws IllegalArgumentException when provider is null
0180: */
0181: public void testKeyStore08() throws KeyStoreException {
0182: assertTrue(NotSupportMsg, JKSSupported);
0183: Provider provider = null;
0184: for (int i = 0; i < validValues.length; i++) {
0185: try {
0186: KeyStore.getInstance(validValues[i], provider);
0187: fail("IllegalArgumentException must be thrown when provider is null (type: "
0188: .concat(validValues[i]).concat(" )"));
0189: } catch (IllegalArgumentException e) {
0190: }
0191: }
0192: }
0193:
0194: /**
0195: * Test for <code>getInstance(String type, Provider provider)</code>
0196: * method
0197: * Assertions:
0198: * throws NullPointerException when type is null
0199: * throws KeyStoreException when type is not available
0200: */
0201: public void testKeyStore09() {
0202: assertTrue(NotSupportMsg, JKSSupported);
0203: try {
0204: KeyStore.getInstance(null, defaultProvider);
0205: fail("KeyStoreException must be thrown when type is null");
0206: } catch (KeyStoreException e) {
0207: } catch (NullPointerException e) {
0208: }
0209: for (int i = 0; i < invalidValues.length; i++) {
0210: try {
0211: KeyStore.getInstance(invalidValues[i], defaultProvider);
0212: fail("KeyStoreException must be thrown when type is null (type: "
0213: .concat(invalidValues[i]).concat(" provider: ")
0214: .concat(defaultProvider.getName()).concat(" )"));
0215: } catch (KeyStoreException e) {
0216: }
0217: }
0218: }
0219:
0220: /**
0221: * Test for <code>getInstance(String type, Provider provider)</code>
0222: * method
0223: * Assertion: returns KeyStore object
0224: */
0225: public void testKeyStore10() throws KeyStoreException {
0226: assertTrue(NotSupportMsg, JKSSupported);
0227: KeyStore ks;
0228: for (int i = 0; i < validValues.length; i++) {
0229: ks = KeyStore.getInstance(validValues[i], defaultProvider);
0230: assertEquals("Incorrect type", ks.getType(), validValues[i]);
0231: assertEquals("Incorrect provider", ks.getProvider(),
0232: defaultProvider);
0233: }
0234: }
0235:
0236: /**
0237: * Test for methods:
0238: * <code>getKey(String alias, char[] password)</code>
0239: * <code>getCertificateChain(String alias)</code>
0240: * <code>getCertificate(String alias)</code>
0241: * <code>getCreationDate(String alias)</code>
0242: * <code>setKeyEntry(String alias, Key key, char[] password, Certificate[] chain)</code>
0243: * <code>setKeyEntry(String alias, byte[] key, Certificate[] chain)</code>
0244: * <code>setCertificateEntry(String alias, Certificate cert)</code>
0245: * <code>deleteEntry(String alias)</code>
0246: * <code>Enumeration aliases()</code>
0247: * <code>containsAlias(String alias)</code>
0248: * <code>size()</code>
0249: * <code>isKeyEntry(String alias)</code>
0250: * <code>isCertificateEntry(String alias)</code>
0251: * <code>getCertificateAlias(Certificate cert)</code>
0252: * <code>store(OutputStream stream, char[] password)</code>
0253: * Assertion: throws KeyStoreException when KeyStore was not initialized
0254: */
0255: public void testKeyStore11() throws Exception {
0256: assertTrue(NotSupportMsg, JKSSupported);
0257: String msgF = "KeyStoreException must be thrown because KeyStore was not initialized";
0258: KeyStore[] kss = createKS();
0259: assertNotNull("KeyStore objects were not created", kss);
0260: for (int i = 0; i < kss.length; i++) {
0261: try {
0262: kss[i].getKey("", new char[1]);
0263: fail(msgF);
0264: } catch (KeyStoreException e) {
0265: }
0266: try {
0267: kss[i].getCertificateChain("");
0268: fail(msgF);
0269: } catch (KeyStoreException e) {
0270: }
0271: try {
0272: kss[i].getCertificate("");
0273: fail(msgF);
0274: } catch (KeyStoreException e) {
0275: }
0276: try {
0277: kss[i].getCreationDate("");
0278: fail(msgF);
0279: } catch (KeyStoreException e) {
0280: }
0281: try {
0282: kss[i].aliases();
0283: fail(msgF);
0284: } catch (KeyStoreException e) {
0285: }
0286: try {
0287: kss[i].containsAlias("");
0288: fail(msgF);
0289: } catch (KeyStoreException e) {
0290: }
0291: try {
0292: kss[i].size();
0293: fail(msgF);
0294: } catch (KeyStoreException e) {
0295: }
0296: try {
0297: kss[i].setKeyEntry("", null, new char[0],
0298: new Certificate[0]);
0299: fail(msgF);
0300: } catch (KeyStoreException e) {
0301: }
0302: try {
0303: kss[i].setKeyEntry("", new byte[0], new Certificate[0]);
0304: fail(msgF);
0305: } catch (KeyStoreException e) {
0306: }
0307: try {
0308: kss[i].setCertificateEntry("", null);
0309: fail(msgF);
0310: } catch (KeyStoreException e) {
0311: }
0312: try {
0313: kss[i].deleteEntry("");
0314: fail(msgF);
0315: } catch (KeyStoreException e) {
0316: }
0317: try {
0318: kss[i].isKeyEntry("");
0319: fail(msgF);
0320: } catch (KeyStoreException e) {
0321: }
0322: try {
0323: kss[i].isCertificateEntry("");
0324: fail(msgF);
0325: } catch (KeyStoreException e) {
0326: }
0327: try {
0328: kss[i].getCertificateAlias(null);
0329: fail(msgF);
0330: } catch (KeyStoreException e) {
0331: }
0332: ByteArrayOutputStream ba = new ByteArrayOutputStream();
0333: try {
0334: kss[i].store(ba, new char[0]);
0335: fail(msgF);
0336: } catch (KeyStoreException e) {
0337: }
0338: try {
0339: kss[i].store(new MyLoadStoreParams(
0340: new KeyStore.PasswordProtection(new char[0])));
0341: fail(msgF);
0342: } catch (KeyStoreException e) {
0343: }
0344: KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(
0345: new KeyStoreTestSupport.MCertificate("type",
0346: new byte[0]));
0347: try {
0348: kss[i].setEntry("aaa", entry, null);
0349: fail(msgF);
0350: } catch (KeyStoreException e) {
0351: }
0352: try {
0353: kss[i].getEntry("aaa", null);
0354: fail(msgF);
0355: } catch (KeyStoreException e) {
0356: }
0357: }
0358: }
0359:
0360: /**
0361: * Test for
0362: * <code>setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter params)</code>
0363: * <code>containsAlias(String alias)</code>
0364: * <code>getEntry(String alias)</code>
0365: * <code>getCertificate(String alias)</code>
0366: * <code>isCertificateEntry(String alias)</code>
0367: * <code>isKeyEntry(String alias)</code>
0368: * methods Assertions: setEntry(..) throws NullPointerException when alias
0369: * or entry is null;
0370: *
0371: * containsAlias(..), getEntry(..), isCertificateEntry(..), isKeyEntry(...)
0372: * throw NullPointerException when alias is null;
0373: *
0374: * setEntry(..) stores Entry and getEntry(..) returns it when
0375: * KeyStore.TrustedCertificateEntry is used; getCertificate(...) returns
0376: * used trusted certificate.
0377: *
0378: */
0379: public void testEntry01() throws Exception {
0380: assertTrue(NotSupportMsg, JKSSupported);
0381: KeyStoreTestSupport.MCertificate trust = new KeyStoreTestSupport.MCertificate(
0382: "type", new byte[0]);
0383: KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(
0384: trust);
0385: KeyStore[] kss = createKS();
0386: assertNotNull("KeyStore objects were not created", kss);
0387:
0388: for (int i = 0; i < kss.length; i++) {
0389: kss[i].load(null, null);
0390: try {
0391: kss[i].setEntry(null, entry, null);
0392: fail("NullPointerException should be thrown when alias is null");
0393: } catch (NullPointerException e) {
0394: }
0395: try {
0396: kss[i].setEntry("ZZZ", null, null);
0397: fail("NullPointerException should be thrown when entry is null");
0398: } catch (NullPointerException e) {
0399: }
0400: for (int j = 0; j < aliases.length; j++) {
0401: kss[i].setEntry(aliases[j], entry, null);
0402: }
0403: }
0404: for (int i = 0; i < kss.length; i++) {
0405: try {
0406: kss[i].containsAlias(null);
0407: fail("NullPointerException should be thrown when alias is null");
0408: } catch (NullPointerException e) {
0409: }
0410: try {
0411: kss[i].isCertificateEntry(null);
0412: fail("NullPointerException should be thrown when alias is null");
0413: } catch (NullPointerException e) {
0414: }
0415: try {
0416: kss[i].isKeyEntry(null);
0417: fail("NullPointerException should be thrown when alias is null");
0418: } catch (NullPointerException e) {
0419: }
0420: try {
0421: kss[i].getEntry(null, null);
0422: fail("NullPointerException should be thrown when alias is null");
0423: } catch (NullPointerException e) {
0424: }
0425: KeyStore.Entry en;
0426: for (int j = 0; j < aliases.length; j++) {
0427: assertFalse("Incorrect alias", kss[i]
0428: .containsAlias("Bad".concat(aliases[j])));
0429: assertTrue("Incorrect alias", kss[i]
0430: .containsAlias(aliases[j]));
0431: assertTrue("Not CertificateEntry", kss[i]
0432: .isCertificateEntry(aliases[j]));
0433: assertFalse("Incorrect KeyEntry", kss[i]
0434: .isKeyEntry(aliases[j]));
0435: en = kss[i].getEntry(aliases[j], null);
0436: assertTrue("Incorrect Entry",
0437: en instanceof KeyStore.TrustedCertificateEntry);
0438: assertEquals("Incorrect certificate",
0439: ((KeyStore.TrustedCertificateEntry) en)
0440: .getTrustedCertificate(), entry
0441: .getTrustedCertificate());
0442: assertEquals("Incorrect certificate", kss[i]
0443: .getCertificate(aliases[j]), trust);
0444: }
0445: }
0446: }
0447:
0448: /**
0449: * Test for
0450: * <code>setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter params)</code>
0451: * <code>containsAlias(String alias)</code>
0452: * <code>getEntry(String alias)</code>
0453: * <code>isCertificateEntry(String alias)</code>
0454: * <code>isKeyEntry(String alias)</code>
0455: * methods
0456: * Assertions:
0457: * getEntry(...) throws KeyStoreException if password is incorrect;
0458: * setEntry(..) throws KeyStoreException if password is destroyed;
0459: *
0460: * setEntry(..) throws KeyStoreException when incorrect Entry is used;
0461: *
0462: * setEntry(..) stores Entry and getEntry(...) returns it when
0463: * KeyStore.PrivateKeyEntry is used.
0464: *
0465: */
0466: public void testEntry02() throws Exception {
0467: assertTrue(NotSupportMsg, JKSSupported);
0468: TestKeyPair tkp = new TestKeyPair("DSA");
0469: KeyStoreTestSupport.MCertificate certs[] = {
0470: new KeyStoreTestSupport.MCertificate("DSA", tkp
0471: .getPrivate().getEncoded()),
0472: new KeyStoreTestSupport.MCertificate("DSA", tkp
0473: .getPrivate().getEncoded()) };
0474: PrivateKey privKey = tkp.getPrivate();
0475: KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(
0476: privKey, certs);
0477: char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
0478: KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(
0479: pwd);
0480: KeyStore.PasswordProtection anotherPath = new KeyStore.PasswordProtection(
0481: new char[0]);
0482: KeyStoreTestSupport.ProtPar pPar = new KeyStoreTestSupport.ProtPar();
0483: KeyStore[] kss = createKS();
0484: assertNotNull("KeyStore objects were not created", kss);
0485: for (int i = 0; i < kss.length; i++) {
0486: kss[i].load(null, null);
0487: for (int j = 0; j < aliases.length; j++) {
0488: kss[i].setEntry(aliases[j], pKey, pPath);
0489: }
0490: KeyStore.Entry en;
0491: Certificate[] cc;
0492: for (int j = 0; j < aliases.length; j++) {
0493: assertTrue("Incorrect alias", kss[i]
0494: .containsAlias(aliases[j]));
0495: assertTrue("Not KeyEntry", kss[i]
0496: .isKeyEntry(aliases[j]));
0497: assertFalse("Incorrect CertificateEntry", kss[i]
0498: .isCertificateEntry(aliases[j]));
0499:
0500: en = kss[i].getEntry(aliases[j], pPath);
0501: assertTrue("Incorrect Entry",
0502: en instanceof KeyStore.PrivateKeyEntry);
0503: Key key = pKey.getPrivateKey();
0504: Key key1 = ((KeyStore.PrivateKeyEntry) en)
0505: .getPrivateKey();
0506: if (!key.getAlgorithm().equals(key1.getAlgorithm())
0507: || !key.getFormat().equals(key1.getFormat())) {
0508: fail("Incorrect key");
0509: }
0510: byte[] enc = key.getEncoded();
0511: byte[] enc1 = key1.getEncoded();
0512: assertTrue("Diff. keys encoding", Arrays.equals(enc,
0513: enc1));
0514:
0515: cc = ((KeyStore.PrivateKeyEntry) en)
0516: .getCertificateChain();
0517: assertEquals("Incorrect CertificateChain", cc.length,
0518: certs.length);
0519: for (int t = 0; t < cc.length; t++) {
0520: assertEquals("Incorrect CertificateChain", cc[t],
0521: certs[t]);
0522: }
0523:
0524: key = kss[i].getKey(aliases[j], pwd);
0525: key1 = privKey;
0526: if (!key.getAlgorithm().equals(key1.getAlgorithm())
0527: || !key.getFormat().equals(key1.getFormat())) {
0528: fail("Incorrect Entry: key");
0529: }
0530: enc = key.getEncoded();
0531: enc1 = key1.getEncoded();
0532: assertTrue("Incorrect Entry: Diff. keys encoding",
0533: Arrays.equals(enc, enc1));
0534:
0535: cc = kss[i].getCertificateChain(aliases[j]);
0536: assertEquals("Incorrect CertificateChain", cc.length,
0537: certs.length);
0538: for (int t = 0; t < cc.length; t++) {
0539: assertEquals("Incorrect CertificateChain", cc[t],
0540: certs[t]);
0541: }
0542: try {
0543: kss[i].getEntry(aliases[j], anotherPath);
0544: fail("KeyStoreException or UnrecoverableEntryException should be thrown "
0545: + "because password is incorrect");
0546: } catch (KeyStoreException e) {
0547: } catch (UnrecoverableEntryException e) {
0548: }
0549: }
0550: }
0551: pPath.destroy();
0552: for (int i = 0; i < kss.length; i++) {
0553: try {
0554: kss[i].setEntry("ZZZ", pKey, pPath);
0555: fail("KeyStoreException should be thrown because password is destroyed");
0556: } catch (KeyStoreException e) {
0557: }
0558:
0559: for (int j = 0; j < aliases.length; j++) {
0560: try {
0561: kss[i].getEntry(aliases[j], pPath);
0562: fail("KeyStoreException should be thrown because password is destroyed");
0563: } catch (KeyStoreException e) {
0564: }
0565:
0566: try {
0567: kss[i].getEntry(aliases[j], pPar);
0568: fail("UnrecoverableEntryException should be thrown");
0569: } catch (UnrecoverableEntryException e) {
0570: }
0571: }
0572: }
0573: }
0574:
0575: /**
0576: * Test for
0577: * <code>setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter params)</code>
0578: * <code>containsAlias(String alias)</code>
0579: * <code>getEntry(String alias)</code>
0580: * <code>isCertificateEntry(String alias)</code>
0581: * <code>isKeyEntry(String alias)</code>
0582: * methods
0583: * Assertions:
0584: * setEntry(..) stores used entry and getEntry(..) returns it when
0585: * KeyStore.SecretKeyEntry is used;
0586: *
0587: * setEntry(..) throws KeyStoreException when incorrect Entry is used.
0588: *
0589: * FIXME: this test should be changed to verify SecretKeyEntry.
0590: * It is not supported.
0591: */
0592: public void testEntry03() throws Exception {
0593: assertTrue(NotSupportMsg, JKSSupported);
0594: TestKeyPair tkp = new TestKeyPair("DSA");
0595: KeyStoreTestSupport.SKey secKey = new KeyStoreTestSupport.SKey(
0596: "DSA", tkp.getPrivate().getEncoded());
0597: KeyStore.SecretKeyEntry sKey = new KeyStore.SecretKeyEntry(
0598: secKey);
0599: char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
0600: KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(
0601: pwd);
0602: KeyStoreTestSupport.AnotherEntry aEntry = new KeyStoreTestSupport.AnotherEntry();
0603: KeyStoreTestSupport.ProtPar pPar = new KeyStoreTestSupport.ProtPar();
0604: KeyStore[] kss = createKS();
0605: assertNotNull("KeyStore objects were not created", kss);
0606: for (int i = 0; i < kss.length; i++) {
0607: kss[i].load(null, null);
0608: for (int j = 0; j < aliases.length; j++) {
0609: try {
0610: kss[i].setEntry(aliases[j], sKey, pPath);
0611: } catch (KeyStoreException e) {
0612: //logln("testEntry03: non-PrivateKeys not supported.");
0613: return;
0614: }
0615: }
0616:
0617: for (int j = 0; j < aliases.length; j++) {
0618: assertTrue("Incorrect alias", kss[i]
0619: .containsAlias(aliases[j]));
0620: assertTrue("Not KeyEntry", kss[i]
0621: .isKeyEntry(aliases[j]));
0622: assertFalse("Incorrect CertificateEntry", kss[i]
0623: .isCertificateEntry(aliases[j]));
0624: Key key1;
0625: try {
0626: key1 = kss[i].getKey(aliases[j], pwd);
0627: } catch (UnrecoverableKeyException e) {
0628: //logln("testEntry03: non-PrivateKeys not supported.");
0629: return;
0630: }
0631: if (!secKey.getAlgorithm().equals(key1.getAlgorithm())
0632: || !secKey.getFormat().equals(key1.getFormat())) {
0633: fail("Incorrect key");
0634: }
0635: byte[] enc = secKey.getEncoded();
0636: byte[] enc1 = key1.getEncoded();
0637: assertTrue("Diff. keys encoding", Arrays.equals(enc,
0638: enc1));
0639: assertNull("Incorrect CertificateChain", kss[i]
0640: .getCertificateChain(aliases[j]));
0641: }
0642: }
0643: pPath.destroy();
0644: for (int i = 0; i < kss.length; i++) {
0645: try {
0646: kss[i].setEntry("ZZZ", aEntry, pPath);
0647: fail("KeyStoreException should be thrown because password is destroyed");
0648: } catch (KeyStoreException e) {
0649: }
0650: for (int j = 0; j < aliases.length; j++) {
0651: try {
0652: kss[i].getEntry(aliases[j], pPath);
0653: fail("KeyStoreException should be thrown because password is destroyed");
0654: } catch (KeyStoreException e) {
0655: }
0656: try {
0657: kss[i].getEntry(aliases[j], pPar);
0658: fail("UnrecoverableEntryException should be thrown");
0659: } catch (UnrecoverableEntryException e) {
0660: }
0661: }
0662: }
0663: }
0664:
0665: /**
0666: * Test for
0667: * <code>setCertificateEntry(String alias, Certificate cert)</code>
0668: * <code>containsAlias(String alias)</code>
0669: * <code>getCertificate(String alias)</code>
0670: * <code>isCertificateEntry(String alias)</code>
0671: * methods
0672: * Assertions:
0673: * setCertificateEntry(..), containsAlias(..), getCertificate(..) and isCertificateEntry(..)
0674: * throw NullPointerException when alias is null
0675: *
0676: * setCertificateEntry(..) stores used entry and getCertificate(..) returns it
0677: *
0678: */
0679: public void testEntry04() throws Exception {
0680: assertTrue(NotSupportMsg, JKSSupported);
0681: KeyStoreTestSupport.MCertificate cert = new KeyStoreTestSupport.MCertificate(
0682: "type", new byte[0]);
0683: KeyStore[] kss = createKS();
0684: assertNotNull("KeyStore objects were not created", kss);
0685:
0686: for (int i = 0; i < kss.length; i++) {
0687: kss[i].load(null, null);
0688: try {
0689: kss[i].setCertificateEntry(null, cert);
0690: fail("NullPointerException should be thrown when alias is null");
0691: } catch (NullPointerException e) {
0692: }
0693: for (int j = 0; j < aliases.length; j++) {
0694: kss[i].setCertificateEntry(aliases[j], cert);
0695: }
0696: }
0697: for (int i = 0; i < kss.length; i++) {
0698: try {
0699: kss[i].containsAlias(null);
0700: fail("NullPointerException should be thrown when alias is null");
0701: } catch (NullPointerException e) {
0702: }
0703: try {
0704: kss[i].isCertificateEntry(null);
0705: fail("NullPointerException should be thrown when alias is null");
0706: } catch (NullPointerException e) {
0707: }
0708: try {
0709: kss[i].getCertificate(null);
0710: fail("NullPointerException should be thrown when alias is null");
0711: } catch (NullPointerException e) {
0712: }
0713: for (int j = 0; j < aliases.length; j++) {
0714: assertFalse("Incorrect alias", kss[i]
0715: .containsAlias("Bad".concat(aliases[j])));
0716: assertTrue("Incorrect alias", kss[i]
0717: .containsAlias(aliases[j]));
0718: assertTrue("Not CertificateEntry", kss[i]
0719: .isCertificateEntry(aliases[j]));
0720: assertFalse("Incorrect KeyEntry", kss[i]
0721: .isKeyEntry(aliases[j]));
0722: assertEquals("Incorrect Certificate", kss[i]
0723: .getCertificate(aliases[j]), cert);
0724: }
0725: }
0726: }
0727:
0728: /**
0729: * Test for
0730: * <code>setKeyEntry(String alias, Key key, char[] password, Certificate[] chain)</code>
0731: * <code>containsAlias(String alias)</code>
0732: * <code>getKey(String alias, char[] password)</code>
0733: * <code>isCertificateEntry(String alias)</code>
0734: * <code>isKeyEntry(String alias)</code>
0735: * <code>setCerificateEntry(String alias, Certificate cert)</code>
0736: * <code>getCertificateChain(String alias)</code>
0737: * <code>getCertificateAlias(Certificate cert)</code>
0738: * methods
0739: *
0740: * Assertions:
0741: * setKeyEntry(..), getKeyEntry(..) and isKeyEntry(..)
0742: * throw NullPointerException when alias is null
0743: *
0744: * setKeyEntry(...) throws KeyStoreException when key or password
0745: * is null
0746: *
0747: * setCertificateEntry(..) throws KeyStoreException when KeyEntry was overwritten
0748: *
0749: * setKeyEntry(..) stores used entry, getKey(..) returns it and getCertificateChain(...)
0750: * returns cert
0751: *
0752: */
0753: public void testEntry05() throws Exception {
0754: assertTrue(NotSupportMsg, JKSSupported);
0755: KeyStoreTestSupport.MCertificate certs[] = {
0756: new KeyStoreTestSupport.MCertificate("type1",
0757: new byte[10]),
0758: new KeyStoreTestSupport.MCertificate("type2",
0759: new byte[10]) };
0760: KeyStoreTestSupport.MCertificate cert = new KeyStoreTestSupport.MCertificate(
0761: "type", new byte[0]);
0762: char[] pwd = new char[0];
0763: TestKeyPair tkp = new TestKeyPair("DSA");
0764: PrivateKey key = tkp.getPrivate();
0765: KeyStore[] kss = createKS();
0766: assertNotNull("KeyStore objects were not created", kss);
0767: for (int i = 0; i < kss.length; i++) {
0768: kss[i].load(null, null);
0769:
0770: // Null as alias does not necessarily lead to NullPointerException
0771:
0772: try {
0773: kss[i].setKeyEntry("ZZZ", null, pwd, certs);
0774: fail("KeyStoreException should be thrown when key is null");
0775: } catch (KeyStoreException e) {
0776: }
0777: try {
0778: kss[i].setKeyEntry("ZZZ", key, pwd, null);
0779: fail("KeyStoreException or IllegalArgumentException should be thrown "
0780: + "when chain is null and key is private");
0781: } catch (IllegalArgumentException e) {
0782: }
0783: try {
0784: kss[i].setKeyEntry("ZZZ", key, pwd,
0785: new KeyStoreTestSupport.MCertificate[0]);
0786: fail("KeyStoreException or IllegalArgumentException should be thrown "
0787: + "when chain is empty and key is private");
0788: } catch (IllegalArgumentException e) {
0789: }
0790:
0791: for (int j = 0; j < aliases.length; j++) {
0792: kss[i].setKeyEntry(aliases[j], key, pwd, certs);
0793: }
0794:
0795: kss[i].setKeyEntry("KeyAlias", key, pwd, certs);
0796: try {
0797: kss[i].setCertificateEntry("KeyAlias", cert);
0798: fail("KeyStoreException should be thrown when we try to overwrite KeyEntry to Certificate");
0799: } catch (KeyStoreException e) {
0800: }
0801:
0802: try {
0803: kss[i].isKeyEntry(null);
0804: fail("NullPointerException should be thrown when alias is null");
0805: } catch (NullPointerException e) {
0806: }
0807: try {
0808: kss[i].getKey(null, pwd);
0809: fail("NullPointerException should be thrown when alias is null");
0810: } catch (NullPointerException e) {
0811: }
0812: try {
0813: kss[i].getCertificateChain(null);
0814: fail("NullPointerException should be thrown when alias is null");
0815: } catch (NullPointerException e) {
0816: }
0817: for (int j = 0; j < aliases.length; j++) {
0818: assertFalse("Incorrect alias", kss[i]
0819: .containsAlias("Bad".concat(aliases[j])));
0820: assertTrue("Incorrect alias", kss[i]
0821: .containsAlias(aliases[j]));
0822: assertTrue("Not KeyEntry", kss[i]
0823: .isKeyEntry(aliases[j]));
0824: assertFalse("Incorrect CertificateEntry", kss[i]
0825: .isCertificateEntry(aliases[j]));
0826: Key key1 = kss[i].getKey(aliases[j], pwd);
0827: if (!key.getAlgorithm().equals(key1.getAlgorithm())
0828: || !key.getFormat().equals(key1.getFormat())) {
0829: fail("Incorrect key");
0830: }
0831: byte[] enc = key.getEncoded();
0832: byte[] enc1 = key1.getEncoded();
0833: assertTrue("Diff. keys encoding", Arrays.equals(enc,
0834: enc1));
0835: Certificate[] cc = kss[i]
0836: .getCertificateChain(aliases[j]);
0837: assertEquals("Incorrect chain", cc.length, certs.length);
0838: for (int t = 0; t < cc.length; t++) {
0839: assertEquals("Incorrect certificate", cc[t],
0840: certs[t]);
0841: }
0842: }
0843: assertNull(kss[i].getCertificateAlias(cert));
0844: String ss = kss[i].getCertificateAlias(certs[0]);
0845: boolean ans = false;
0846: for (int j = 1; j < aliases.length; j++) {
0847: if (ss.equals(aliases[j])) {
0848: ans = true;
0849: break;
0850: }
0851: }
0852: assertTrue(
0853: "There is no alias for certificate <type1, new byte[10]>",
0854: ans);
0855: }
0856: }
0857:
0858: /**
0859: * Test for
0860: * <code>deleteEntry(String alias)</code>
0861: * <code>size()</code>
0862: * methods
0863: * Assertions:
0864: * throws NullPointerException when alias is null;
0865: *
0866: * deletes entry from KeyStore.
0867: *
0868: */
0869: public void testEntry06() throws Exception {
0870: assertTrue(NotSupportMsg, JKSSupported);
0871: KeyStore.TrustedCertificateEntry tCert = new KeyStore.TrustedCertificateEntry(
0872: new KeyStoreTestSupport.MCertificate("type",
0873: new byte[0]));
0874:
0875: TestKeyPair tkp = new TestKeyPair("DSA");
0876: KeyStoreTestSupport.MCertificate certs[] = {
0877: new KeyStoreTestSupport.MCertificate("DSA", tkp
0878: .getPrivate().getEncoded()),
0879: new KeyStoreTestSupport.MCertificate("DSA", tkp
0880: .getPrivate().getEncoded()) };
0881: KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(
0882: tkp.getPrivate(), certs);
0883: char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
0884: KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(
0885: pwd);
0886:
0887: String[] aliases = { "Alias1", "Alias2", "Alias3", "Alias4",
0888: "Alias5" };
0889:
0890: KeyStore[] kss = createKS();
0891: assertNotNull("KeyStore objects were not created", kss);
0892:
0893: for (int i = 0; i < kss.length; i++) {
0894: kss[i].load(null, null);
0895: kss[i].setEntry(aliases[0], tCert, null);
0896: kss[i].setEntry(aliases[1], pKey, pp);
0897: kss[i].setEntry(aliases[2], pKey, pp);
0898:
0899: kss[i]
0900: .setKeyEntry(aliases[3], tkp.getPrivate(), pwd,
0901: certs);
0902:
0903: kss[i].setCertificateEntry(aliases[4], certs[0]);
0904:
0905: assertEquals("Incorrect size", kss[i].size(), 5);
0906: try {
0907: kss[i].deleteEntry(null);
0908: fail("NullPointerException should be thrown when alias is null");
0909: } catch (NullPointerException e) {
0910: }
0911: kss[i].deleteEntry(aliases[0]);
0912: kss[i].deleteEntry(aliases[3]);
0913: assertEquals("Incorrect size", kss[i].size(), 3);
0914: for (int j = 1; j < 5; j++) {
0915: if ((j == 0) || (j == 3)) {
0916: assertFalse("Incorrect deleted alias", kss[i]
0917: .containsAlias(aliases[j]));
0918: } else {
0919: assertTrue("Incorrect alias", kss[i]
0920: .containsAlias(aliases[j]));
0921: }
0922: }
0923: }
0924: }
0925:
0926: /**
0927: * Test for
0928: * <code>entryInstanceOf(String alias, Class class)</code>
0929: * method
0930: * Assertions:
0931: * throws NullPointerException when alias is null
0932: * returns false if KeyStore does not contain entry with defined alias
0933: * returns false if defined alias is not correspond Entry
0934: * returns false
0935: * setEntry(..) throws KeyStoreException when incorrect Entry is used;
0936: *
0937: * setEntry(..) stores Entry and getEntry(...) returns it when
0938: * KeyStore.PrivateKeyEntry is used.
0939: *
0940: */
0941: public void testEntry07() throws Exception {
0942: assertTrue(NotSupportMsg, JKSSupported);
0943: TestKeyPair tkp = new TestKeyPair("DSA");
0944: KeyStoreTestSupport.MCertificate certs[] = {
0945: new KeyStoreTestSupport.MCertificate("DSA", tkp
0946: .getPrivate().getEncoded()),
0947: new KeyStoreTestSupport.MCertificate("DSA", tkp
0948: .getPrivate().getEncoded()) };
0949: PrivateKey privKey = tkp.getPrivate();
0950: KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(
0951: privKey, certs);
0952: char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
0953: String aliasKE = "KeyAlias";
0954: KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(
0955: pwd);
0956: new KeyStore.PasswordProtection(new char[0]);
0957: KeyStore[] kss = createKS();
0958: assertNotNull("KeyStore objects were not created", kss);
0959:
0960: for (int i = 0; i < kss.length; i++) {
0961: kss[i].load(null, null);
0962: // set entries
0963: for (int j = 0; j < aliases.length; j++) {
0964: kss[i].setEntry(aliases[j], pKey, pp);
0965: }
0966: kss[i].setKeyEntry(aliasKE, privKey, pwd, certs);
0967: try {
0968: kss[i].entryInstanceOf(null, pKey.getClass());
0969: fail("NullPointerEXception must be thrown");
0970: } catch (NullPointerException e) {
0971: }
0972: assertFalse("Incorrect class entry 1", kss[i]
0973: .entryInstanceOf("ZZZ", pKey.getClass()));
0974: for (int j = 0; j < aliases.length; j++) {
0975: assertTrue("Incorrect class entry 2", kss[i]
0976: .entryInstanceOf(aliases[j], pKey.getClass()));
0977:
0978: //make it compilable on 1.5
0979: Class c = privKey.getClass();
0980: assertFalse("Incorrect class entry 3", kss[i]
0981: .entryInstanceOf(aliases[j], c));
0982: }
0983:
0984: //make it compilable on 1.5
0985: Class c = privKey.getClass();
0986: assertFalse("Incorrect class entry 4", kss[i]
0987: .entryInstanceOf(aliasKE, c));
0988: assertTrue("Incorrect class entry 5", kss[i]
0989: .entryInstanceOf(aliasKE, pKey.getClass()));
0990: }
0991: }
0992:
0993: /**
0994: * Test for <code>KeyStore(KeyStoreSpi spi, Provider prov, String type)</code>
0995: * constructor
0996: * Assertion: constructs KeyStore object
0997: */
0998: public void testKeyStoreConstr() throws Exception {
0999: assertTrue(NotSupportMsg, JKSSupported);
1000: KeyStoreSpi spi = new MyKeyStoreSpi();
1001: KeyStore keySt = new tmpKeyStore(spi, defaultProvider,
1002: defaultType);
1003: assertEquals("Incorrect name", keySt.getType(), defaultType);
1004: assertEquals("Incorrect provider", keySt.getProvider(),
1005: defaultProvider);
1006: char[] pwd = new char[0];
1007: try {
1008: keySt.store(null, pwd);
1009: fail("KeyStoreException must be thrown");
1010: } catch (KeyStoreException e) {
1011: }
1012: keySt = new tmpKeyStore(null, null, null);
1013: assertNull("Algorithm must be null", keySt.getType());
1014: assertNull("Provider must be null", keySt.getProvider());
1015: try {
1016: keySt.load(null, pwd);
1017: fail("NullPointerException must be thrown");
1018: } catch (NullPointerException e) {
1019: }
1020: }
1021:
1022: }
1023:
1024: /**
1025: * Additional class to verify KeyStore constructor
1026: */
1027: class tmpKeyStore extends KeyStore {
1028: public tmpKeyStore(KeyStoreSpi spi, Provider prov, String alg) {
1029: super(spi, prov, alg);
1030: }
1031: }
|