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.security.*;
024: import java.security.cert.Certificate;
025:
026: import org.apache.harmony.security.tests.support.cert.MyCertificate;
027:
028: import junit.framework.TestCase;
029:
030: import junit.framework.Test;
031: import junit.framework.TestSuite;
032:
033: /**
034: * Tests for <code>KeyStore.PrivateKeyEntry</code> class constructor and methods
035: *
036: */
037:
038: public class KSPrivateKeyEntryTest extends TestCase {
039:
040: /**
041: * Constructor for KSPrivateKeyEntryTest.
042: * @param arg0
043: */
044: public KSPrivateKeyEntryTest(String arg0) {
045: super (arg0);
046: }
047:
048: private PrivateKey testPrivateKey;
049: private Certificate[] testChain;
050:
051: private void createParams(boolean diffCerts, boolean diffKeys) {
052: byte[] encoded = { (byte) 0, (byte) 1, (byte) 2, (byte) 3 };
053: testChain = new Certificate[5];
054: for (int i = 0; i < testChain.length; i++) {
055: String s = (diffCerts ? Integer.toString(i) : "NEW");
056: testChain[i] = new MyCertificate("MY_TEST_CERTIFICATE_"
057: .concat(s), encoded);
058: }
059: testPrivateKey = (diffKeys ? (PrivateKey) new tmpPrivateKey()
060: : (PrivateKey) new tmpPrivateKey(testChain[0]
061: .getPublicKey().getAlgorithm()));
062: }
063:
064: /**
065: * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
066: * constructor
067: * Assertion: throws NullPointerException when privateKey is null
068: */
069: public void testPrivateKeyEntry01() {
070: Certificate[] certs = new MyCertificate[1];//new Certificate[1];
071: PrivateKey pk = null;
072: try {
073: new KeyStore.PrivateKeyEntry(pk, certs);
074: fail("NullPointerException must be thrown when privateKey is null");
075: } catch (NullPointerException e) {
076: }
077: }
078:
079: /**
080: * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
081: * constructor
082: * Assertion: throws NullPointerException when chain is null
083: * and throws IllegalArgumentException when chain length is 0
084: */
085: public void testPrivateKeyEntry02() {
086: Certificate[] chain = null;
087: PrivateKey pk = new tmpPrivateKey();
088: try {
089: new KeyStore.PrivateKeyEntry(pk, chain);
090: fail("NullPointerException must be thrown when chain is null");
091: } catch (NullPointerException e) {
092: }
093: try {
094: chain = new Certificate[0];
095: new KeyStore.PrivateKeyEntry(pk, chain);
096: fail("IllegalArgumentException must be thrown when chain length is 0");
097: } catch (IllegalArgumentException e) {
098: }
099: }
100:
101: /**
102: * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
103: * constructor
104: * Assertion: throws IllegalArgumentException when chain contains certificates
105: * of different types
106: */
107: public void testPrivateKeyEntry03() {
108: createParams(true, false);
109: try {
110: new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
111: fail("IllegalArgumentException must be thrown when chain contains certificates of different types");
112: } catch (IllegalArgumentException e) {
113: }
114: }
115:
116: /**
117: * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
118: * constructor
119: * Assertion: throws IllegalArgumentException when algorithm of privateKey
120: * does not match the algorithm of PublicKey in the end certificate (with 0 index)
121: */
122: public void testPrivateKeyEntry04() {
123: createParams(false, true);
124: try {
125: new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
126: fail("IllegalArgumentException must be thrown when key algorithms do not match");
127: } catch (IllegalArgumentException e) {
128: }
129: }
130:
131: /**
132: * Test for <code>getPrivateKey()</code> method
133: * Assertion: returns PrivateKey object
134: */
135: public void testGetPrivateKey() {
136: createParams(false, false);
137: KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
138: testPrivateKey, testChain);
139: assertEquals("Incorrect PrivateKey", testPrivateKey, ksPKE
140: .getPrivateKey());
141: }
142:
143: /**
144: * Test for <code>getCertificateChain()</code> method Assertion: returns
145: * array of the Certificates corresponding to chain
146: */
147: public void testGetCertificateChain() {
148: createParams(false, false);
149: KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
150: testPrivateKey, testChain);
151: Certificate[] res = ksPKE.getCertificateChain();
152: assertEquals("Incorrect chain length", testChain.length,
153: res.length);
154: for (int i = 0; i < res.length; i++) {
155: assertEquals("Incorrect chain element: ".concat(Integer
156: .toString(i)), testChain[i], res[i]);
157: }
158: }
159:
160: /**
161: * Test for <code>getCertificate()</code> method
162: * Assertion: returns end Certificate (with 0 index in chain)
163: */
164: public void testGetCertificate() {
165: createParams(false, false);
166: KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
167: testPrivateKey, testChain);
168: Certificate res = ksPKE.getCertificate();
169: assertEquals("Incorrect end certificate (number 0)",
170: testChain[0], res);
171: }
172:
173: /**
174: * Test for <code>toString()</code> method
175: * Assertion: returns non null String
176: */
177: public void testToString() {
178: createParams(false, false);
179: KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
180: testPrivateKey, testChain);
181: String res = ksPKE.toString();
182: assertNotNull("toString() returns null", res);
183: }
184:
185: public static Test suite() {
186: return new TestSuite(KSPrivateKeyEntryTest.class);
187: }
188:
189: public static void main(String args[]) {
190: junit.textui.TestRunner.run(suite());
191: }
192:
193: private static class tmpPrivateKey implements PrivateKey {
194: private String alg = "My algorithm";
195:
196: public String getAlgorithm() {
197: return alg;
198: }
199:
200: public String getFormat() {
201: return "My Format";
202: }
203:
204: public byte[] getEncoded() {
205: return new byte[1];
206: }
207:
208: public tmpPrivateKey() {
209: }
210:
211: public tmpPrivateKey(String algorithm) {
212: super();
213: alg = algorithm;
214: }
215: }
216: }
|