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.cert; 23 24 25import java.io.ByteArrayInputStream; 26import java.io.DataInputStream; 27import java.io.InputStream; 28import java.security.cert.CRL; 29import java.security.cert.CRLException; 30import java.security.cert.Certificate; 31import java.security.cert.CertificateException; 32import java.security.cert.CertificateFactorySpi; 33import java.util.Collection; 34import java.util.Iterator; 35import java.util.List; 36 37import org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi; 38 39import junit.framework.TestCase; 40 41/** 42 * Tests for <code>CertificateFactorySpi</code> class constructors and methods 43 * 44 */ 45 46public class CertificateFactorySpiTest extends TestCase { 47 /** 48 * Constructor for CertStoreSpiTest. 49 * 50 * @param arg0 51 */ 52 public CertificateFactorySpiTest(String arg0) { 53 super(arg0); 54 } 55 56 /** 57 * Test for <code>CertificateFactorySpi</code> constructor 58 * Assertion: constructs CertificateFactorySpi 59 */ 60 public void testCertificateFactorySpi01() throws CertificateException, 61 CRLException { 62 CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi(); 63 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 64 try { 65 certFactorySpi.engineGenerateCertPath(bais); 66 fail("UnsupportedOperationException must be thrown"); 67 } catch (UnsupportedOperationException e) { 68 } 69 try { 70 certFactorySpi.engineGenerateCertPath(bais, ""); 71 fail("UnsupportedOperationException must be thrown"); 72 } catch (UnsupportedOperationException e) { 73 } 74 try { 75 List list = null; 76 certFactorySpi.engineGenerateCertPath(list); 77 fail("UnsupportedOperationException must be thrown"); 78 } catch (UnsupportedOperationException e) { 79 } 80 try { 81 certFactorySpi.engineGetCertPathEncodings(); 82 fail("UnsupportedOperationException must be thrown"); 83 } catch (UnsupportedOperationException e) { 84 } 85 Certificate cc = certFactorySpi 86 .engineGenerateCertificate(bais); 87 assertNull("Not null Cerificate", cc); 88 try { 89 certFactorySpi.engineGenerateCertificate(null); 90 fail("CertificateException must be thrown"); 91 } catch (CertificateException e) { 92 } 93 Collection col = certFactorySpi 94 .engineGenerateCertificates(bais); 95 assertNull("Not null Collection", col); 96 try { 97 certFactorySpi.engineGenerateCertificates(null); 98 fail("CertificateException must be thrown"); 99 } catch (CertificateException e) { 100 } 101 102 CRL ccCRL = certFactorySpi.engineGenerateCRL(bais); 103 assertNull("Not null CRL", ccCRL); 104 try { 105 certFactorySpi.engineGenerateCRL(null); 106 fail("CRLException must be thrown"); 107 } catch (CRLException e) { 108 } 109 110 Collection colCRL = certFactorySpi 111 .engineGenerateCRLs(bais); 112 assertNull("Not null CRL", colCRL); 113 try { 114 certFactorySpi.engineGenerateCRLs(null); 115 fail("CRLException must be thrown"); 116 } catch (CRLException e) { 117 } 118 } 119 120 /** 121 * Test for <code>CertificateFactorySpi</code> constructor 122 * Assertion: constructs CertificateFactorySpi 123 */ 124 public void testCertificateFactorySpi02() throws CertificateException, 125 CRLException { 126 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 127 MyCertificateFactorySpi.putMode(true); 128 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 129 DataInputStream dis = new DataInputStream(bais); 130 try { 131 certFactorySpi.engineGenerateCertPath(bais); 132 fail("CertificateException must be thrown"); 133 } catch (CertificateException e) { 134 } 135 certFactorySpi.engineGenerateCertPath(dis); 136 try { 137 certFactorySpi.engineGenerateCertPath(bais, "aa"); 138 fail("CertificateException must be thrown"); 139 } catch (CertificateException e) { 140 } 141 try { 142 certFactorySpi.engineGenerateCertPath(dis, ""); 143 fail("IllegalArgumentException must be thrown"); 144 } catch (IllegalArgumentException e) { 145 } 146 certFactorySpi.engineGenerateCertPath(dis, "ss"); 147 148 try { 149 certFactorySpi.engineGenerateCertificate(bais); 150 fail("CertificateException must be thrown"); 151 } catch (CertificateException e) { 152 } 153 try { 154 certFactorySpi.engineGenerateCertificates(null); 155 fail("CertificateException must be thrown"); 156 } catch (CertificateException e) { 157 } 158 Certificate cert = certFactorySpi 159 .engineGenerateCertificate(dis); 160 assertNull("Result must be null", cert); 161 Collection col = certFactorySpi 162 .engineGenerateCertificates(dis); 163 assertNull("Result must be null", col); 164 165 try { 166 certFactorySpi.engineGenerateCRL(bais); 167 fail("CRLException must be thrown"); 168 } catch (CRLException e) { 169 } 170 try { 171 certFactorySpi.engineGenerateCRLs(null); 172 fail("CRLException must be thrown"); 173 } catch (CRLException e) { 174 } 175 CRL crl = certFactorySpi.engineGenerateCRL(dis); 176 assertNull("Result must be null", crl); 177 col = certFactorySpi.engineGenerateCRLs(dis); 178 assertNull("Result must be null", col); 179 180 List list = null; 181 try { 182 certFactorySpi.engineGenerateCertPath(list); 183 fail("NullPointerException must be thrown"); 184 } catch (NullPointerException e) { 185 } 186 Iterator enc = certFactorySpi.engineGetCertPathEncodings(); 187 assertTrue("Incorrect Iterator", enc.hasNext()); 188 } 189 190 /** 191 * Test for <code>CertificateFactorySpi</code> constructor 192 * Assertion: constructs CertificateFactorySpi 193 */ 194 public void testCertificateFactorySpi03() throws CertificateException, 195 CRLException { 196 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 197 MyCertificateFactorySpi.putMode(false); 198 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 199 DataInputStream dis = new DataInputStream(bais); 200 try { 201 certFactorySpi.engineGenerateCertPath(bais); 202 fail("CertificateException must be thrown"); 203 } catch (CertificateException e) { 204 } 205 try { 206 certFactorySpi.engineGenerateCertPath(dis); 207 fail("CertificateException must be thrown"); 208 } catch (CertificateException e) { 209 } 210 try { 211 certFactorySpi.engineGenerateCertPath(bais, "aa"); 212 fail("CertificateException must be thrown"); 213 } catch (CertificateException e) { 214 } 215 certFactorySpi.engineGenerateCertPath(dis, ""); 216 certFactorySpi.engineGenerateCertPath(dis, "ss"); 217 218 try { 219 certFactorySpi.engineGenerateCertificate(bais); 220 fail("CertificateException must be thrown"); 221 } catch (CertificateException e) { 222 } 223 try { 224 certFactorySpi.engineGenerateCertificates(null); 225 fail("CertificateException must be thrown"); 226 } catch (CertificateException e) { 227 } 228 Certificate cert = certFactorySpi 229 .engineGenerateCertificate(dis); 230 assertNull("Result must be null", cert); 231 Collection col = certFactorySpi 232 .engineGenerateCertificates(dis); 233 assertNull("Result must be null", col); 234 235 try { 236 certFactorySpi.engineGenerateCRL(bais); 237 fail("CRLException must be thrown"); 238 } catch (CRLException e) { 239 } 240 try { 241 certFactorySpi.engineGenerateCRLs(null); 242 fail("CRLException must be thrown"); 243 } catch (CRLException e) { 244 } 245 CRL crl = certFactorySpi.engineGenerateCRL(dis); 246 assertNull("Result must be null", crl); 247 col = certFactorySpi.engineGenerateCRLs(dis); 248 assertNull("Result must be null", col); 249 250 List list = null; 251 certFactorySpi.engineGenerateCertPath(list); 252 Iterator enc = certFactorySpi.engineGetCertPathEncodings(); 253 assertFalse("Incorrect Iterator", enc.hasNext()); 254 } 255 256 257 private static class extCertificateFactorySpi extends CertificateFactorySpi { 258 public Certificate engineGenerateCertificate(InputStream inStream) 259 throws CertificateException { 260 if (inStream == null) { 261 throw new CertificateException("InputStream null"); 262 } 263 return null; 264 } 265 266 public Collection engineGenerateCertificates(InputStream inStream) 267 throws CertificateException { 268 if (inStream == null) { 269 throw new CertificateException("InputStream null"); 270 } 271 return null; 272 } 273 274 public CRL engineGenerateCRL(InputStream inStream) throws CRLException { 275 if (inStream == null) { 276 throw new CRLException("InputStream null"); 277 } 278 return null; 279 } 280 281 public Collection engineGenerateCRLs(InputStream inStream) 282 throws CRLException { 283 if (inStream == null) { 284 throw new CRLException("InputStream null"); 285 } 286 return null; 287 } 288 } 289} 290