1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19 * @author Vera Y. Petrashkova 20 */ 21 22package org.apache.harmony.security.tests.java.security; 23 24import java.security.*; 25import java.security.cert.Certificate; 26 27import org.apache.harmony.security.tests.support.cert.MyCertificate; 28 29import junit.framework.TestCase; 30 31import junit.framework.Test; 32import junit.framework.TestSuite; 33 34/** 35 * Tests for <code>KeyStore.PrivateKeyEntry</code> class constructor and methods 36 */ 37 38public class KSPrivateKeyEntryTest extends TestCase { 39 40 /** 41 * Constructor for KSPrivateKeyEntryTest. 42 * 43 * @param arg0 44 */ 45 public KSPrivateKeyEntryTest(String arg0) { 46 super(arg0); 47 } 48 49 private PrivateKey testPrivateKey; 50 private Certificate[] testChain; 51 52 private void createParams(boolean diffCerts, boolean diffKeys) { 53 byte[] encoded = { (byte) 0, (byte) 1, (byte) 2, (byte) 3 }; 54 testChain = new Certificate[5]; 55 for (int i = 0; i < testChain.length; i++) { 56 String s = (diffCerts ? Integer.toString(i) : "NEW"); 57 testChain[i] = new MyCertificate("MY_TEST_CERTIFICATE_" 58 .concat(s), encoded); 59 } 60 testPrivateKey = (diffKeys ? (PrivateKey) new tmpPrivateKey() : 61 (PrivateKey) new tmpPrivateKey(testChain[0].getPublicKey().getAlgorithm())); 62 } 63 64 /** 65 * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code> 66 * constructor 67 * Assertion: throws NullPointerException when privateKey is null 68 */ 69 public void testPrivateKeyEntry01() { 70 Certificate[] certs = new MyCertificate[1];//new Certificate[1]; 71 PrivateKey pk = null; 72 try { 73 new KeyStore.PrivateKeyEntry(pk, certs); 74 fail("NullPointerException must be thrown when privateKey is null"); 75 } catch (NullPointerException e) { 76 } 77 } 78 79 /** 80 * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code> 81 * constructor 82 * Assertion: throws NullPointerException when chain is null 83 * and throws IllegalArgumentException when chain length is 0 84 */ 85 public void testPrivateKeyEntry02() { 86 Certificate[] chain = null; 87 PrivateKey pk = new tmpPrivateKey(); 88 try { 89 new KeyStore.PrivateKeyEntry(pk, chain); 90 fail("NullPointerException must be thrown when chain is null"); 91 } catch (NullPointerException e) { 92 } 93 try { 94 chain = new Certificate[0]; 95 new KeyStore.PrivateKeyEntry(pk, chain); 96 fail("IllegalArgumentException must be thrown when chain length is 0"); 97 } catch (IllegalArgumentException e) { 98 } 99 } 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, res.length); 153 for (int i = 0; i < res.length; i++) { 154 assertEquals("Incorrect chain element: " 155 .concat(Integer.toString(i)), testChain[i], res[i]); 156 } 157 } 158 159 /** 160 * Test for <code>getCertificate()</code> method 161 * Assertion: returns end Certificate (with 0 index in chain) 162 */ 163 public void testGetCertificate() { 164 createParams(false, false); 165 KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry( 166 testPrivateKey, testChain); 167 Certificate res = ksPKE.getCertificate(); 168 assertEquals("Incorrect end certificate (number 0)", testChain[0], res); 169 } 170 171 /** 172 * Test for <code>toString()</code> method 173 * Assertion: returns non null String 174 */ 175 public void testToString() { 176 createParams(false, false); 177 KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry( 178 testPrivateKey, testChain); 179 String res = ksPKE.toString(); 180 assertNotNull("toString() returns null", res); 181 } 182 183 public static Test suite() { 184 return new TestSuite(KSPrivateKeyEntryTest.class); 185 } 186 187 private static class tmpPrivateKey implements PrivateKey { 188 private String alg = "My algorithm"; 189 190 public String getAlgorithm() { 191 return alg; 192 } 193 194 public String getFormat() { 195 return "My Format"; 196 } 197 198 public byte[] getEncoded() { 199 return new byte[1]; 200 } 201 202 public tmpPrivateKey() { 203 } 204 205 public tmpPrivateKey(String algorithm) { 206 super(); 207 alg = algorithm; 208 } 209 } 210} 211