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 * @version $Revision$ 21 */ 22 23 package tests.security.cert; 24 25 26 import junit.framework.TestCase; 27 28 import org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi; 29 30 import java.io.ByteArrayInputStream; 31 import java.io.DataInputStream; 32 import java.io.InputStream; 33 import java.security.cert.CRL; 34 import java.security.cert.CRLException; 35 import java.security.cert.Certificate; 36 import java.security.cert.CertificateException; 37 import java.security.cert.CertificateFactorySpi; 38 import java.util.ArrayList; 39 import java.util.Collection; 40 import java.util.Iterator; 41 import java.util.List; 42 43 /** 44 * Tests for <code>CertificateFactorySpi</code> class constructors and methods 45 * 46 */ 47 public class CertificateFactorySpiTest extends TestCase { 48 49 /** 50 * Test for <code>CertificateFactorySpi</code> constructor 51 * Assertion: constructs CertificateFactorySpi 52 */ 53 public void testCertificateFactorySpi01() throws CertificateException, 54 CRLException { 55 CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi(); 56 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 57 try { 58 certFactorySpi.engineGenerateCertPath(bais); 59 fail("UnsupportedOperationException must be thrown"); 60 } catch (UnsupportedOperationException e) { 61 } 62 try { 63 certFactorySpi.engineGenerateCertPath(bais, ""); 64 fail("UnsupportedOperationException must be thrown"); 65 } catch (UnsupportedOperationException e) { 66 } 67 try { 68 List<Certificate> list = null; 69 certFactorySpi.engineGenerateCertPath(list); 70 fail("UnsupportedOperationException must be thrown"); 71 } catch (UnsupportedOperationException e) { 72 } 73 try { 74 certFactorySpi.engineGetCertPathEncodings(); 75 fail("UnsupportedOperationException must be thrown"); 76 } catch (UnsupportedOperationException e) { 77 } 78 Certificate cc = certFactorySpi.engineGenerateCertificate(bais); 79 assertNull("Not null Cerificate", cc); 80 try { 81 certFactorySpi.engineGenerateCertificate(null); 82 fail("CertificateException must be thrown"); 83 } catch (CertificateException e) { 84 } 85 Collection<? extends Certificate> col = certFactorySpi.engineGenerateCertificates(bais); 86 assertNull("Not null Collection", col); 87 try { 88 certFactorySpi.engineGenerateCertificates(null); 89 fail("CertificateException must be thrown"); 90 } catch (CertificateException e) { 91 } 92 93 CRL ccCRL = certFactorySpi.engineGenerateCRL(bais); 94 assertNull("Not null CRL", ccCRL); 95 try { 96 certFactorySpi.engineGenerateCRL(null); 97 fail("CRLException must be thrown"); 98 } catch (CRLException e) { 99 } 100 101 Collection<? extends CRL> colCRL = certFactorySpi.engineGenerateCRLs(bais); 102 assertNull("Not null CRL", colCRL); 103 try { 104 certFactorySpi.engineGenerateCRLs(null); 105 fail("CRLException must be thrown"); 106 } catch (CRLException e) { 107 } 108 } 109 110 /** 111 * Test for <code>CertificateFactorySpi</code> constructor 112 * Assertion: constructs CertificateFactorySpi 113 */ 114 public void testCertificateFactorySpi02() throws CertificateException, 115 CRLException { 116 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 117 MyCertificateFactorySpi.putMode(true); 118 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 119 DataInputStream dis = new DataInputStream(bais); 120 try { 121 certFactorySpi.engineGenerateCertPath(bais); 122 fail("CertificateException must be thrown"); 123 } catch (CertificateException e) { 124 } 125 certFactorySpi.engineGenerateCertPath(dis); 126 try { 127 certFactorySpi.engineGenerateCertPath(bais, "aa"); 128 fail("CertificateException must be thrown"); 129 } catch (CertificateException e) { 130 } 131 try { 132 certFactorySpi.engineGenerateCertPath(dis, ""); 133 fail("IllegalArgumentException must be thrown"); 134 } catch (IllegalArgumentException e) { 135 } 136 certFactorySpi.engineGenerateCertPath(dis, "ss"); 137 138 try { 139 certFactorySpi.engineGenerateCertificate(bais); 140 fail("CertificateException must be thrown"); 141 } catch (CertificateException e) { 142 } 143 try { 144 certFactorySpi.engineGenerateCertificates(null); 145 fail("CertificateException must be thrown"); 146 } catch (CertificateException e) { 147 } 148 Certificate cert = certFactorySpi 149 .engineGenerateCertificate(dis); 150 assertNull("Result must be null", cert); 151 Collection<? extends Certificate> col = certFactorySpi 152 .engineGenerateCertificates(dis); 153 assertNull("Result must be null", col); 154 155 try { 156 certFactorySpi.engineGenerateCRL(bais); 157 fail("CRLException must be thrown"); 158 } catch (CRLException e) { 159 } 160 try { 161 certFactorySpi.engineGenerateCRLs(null); 162 fail("CRLException must be thrown"); 163 } catch (CRLException e) { 164 } 165 CRL crl = certFactorySpi.engineGenerateCRL(dis); 166 assertNull("Result must be null", crl); 167 Collection<? extends CRL> colcrl = certFactorySpi.engineGenerateCRLs(dis); 168 assertNull("Result must be null", colcrl); 169 170 List<Certificate> list = null; 171 try { 172 certFactorySpi.engineGenerateCertPath(list); 173 fail("NullPointerException must be thrown"); 174 } catch (NullPointerException e) { 175 } 176 Iterator<String> enc = certFactorySpi.engineGetCertPathEncodings(); 177 assertTrue("Incorrect Iterator", enc.hasNext()); 178 } 179 180 /** 181 * Test for <code>CertificateFactorySpi</code> constructor 182 * Assertion: constructs CertificateFactorySpi 183 */ 184 public void testCertificateFactorySpi03() throws CertificateException, 185 CRLException { 186 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 187 MyCertificateFactorySpi.putMode(false); 188 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 189 DataInputStream dis = new DataInputStream(bais); 190 try { 191 certFactorySpi.engineGenerateCertPath(bais); 192 fail("CertificateException must be thrown"); 193 } catch (CertificateException e) { 194 } 195 try { 196 certFactorySpi.engineGenerateCertPath(dis); 197 fail("CertificateException must be thrown"); 198 } catch (CertificateException e) { 199 } 200 try { 201 certFactorySpi.engineGenerateCertPath(bais, "aa"); 202 fail("CertificateException must be thrown"); 203 } catch (CertificateException e) { 204 } 205 certFactorySpi.engineGenerateCertPath(dis, ""); 206 certFactorySpi.engineGenerateCertPath(dis, "ss"); 207 208 try { 209 certFactorySpi.engineGenerateCertificate(bais); 210 fail("CertificateException must be thrown"); 211 } catch (CertificateException e) { 212 } 213 try { 214 certFactorySpi.engineGenerateCertificates(null); 215 fail("CertificateException must be thrown"); 216 } catch (CertificateException e) { 217 } 218 Certificate cert = certFactorySpi 219 .engineGenerateCertificate(dis); 220 assertNull("Result must be null", cert); 221 Collection<? extends Certificate> col = certFactorySpi 222 .engineGenerateCertificates(dis); 223 assertNull("Result must be null", col); 224 225 try { 226 certFactorySpi.engineGenerateCRL(bais); 227 fail("CRLException must be thrown"); 228 } catch (CRLException e) { 229 } 230 try { 231 certFactorySpi.engineGenerateCRLs(null); 232 fail("CRLException must be thrown"); 233 } catch (CRLException e) { 234 } 235 CRL crl = certFactorySpi.engineGenerateCRL(dis); 236 assertNull("Result must be null", crl); 237 Collection<? extends CRL> colcrl = certFactorySpi.engineGenerateCRLs(dis); 238 assertNull("Result must be null", colcrl); 239 240 List<Certificate> list = null; 241 certFactorySpi.engineGenerateCertPath(list); 242 Iterator<String> enc = certFactorySpi.engineGetCertPathEncodings(); 243 assertFalse("Incorrect Iterator", enc.hasNext()); 244 } 245 246 /** 247 * Test for <code>engineGenerateCertPath(InputStream)</code> method. 248 * Assertion: Generates a <code>CertPath</code> object and initializes it 249 * with the data read from the <code>InputStream</code> 250 */ 251 public void testEngineGenerateCertPathLjava_io_InputStream01() { 252 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 253 254 MyCertificateFactorySpi.putMode(true); 255 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 256 DataInputStream dis = new DataInputStream(bais); 257 258 try { 259 assertNull(certFactorySpi.engineGenerateCertPath(dis)); 260 } catch (CertificateException e) { 261 fail("Unexpected CertificateException " + e.getMessage()); 262 } 263 } 264 265 /** 266 * Test for <code>engineGenerateCertPath(InputStream)</code> method. 267 * Assertion: Generates a <code>CertPath</code> object and initializes it 268 * with the data read from the <code>InputStream</code> 269 */ 270 public void testEngineGenerateCertPathLjava_io_InputStream02() { 271 CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi(); 272 273 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 274 DataInputStream dis = new DataInputStream(bais); 275 276 try { 277 certFactorySpi.engineGenerateCertPath(dis); 278 fail("UnsupportedOperationException expected"); 279 } catch (UnsupportedOperationException e) { 280 // expected 281 } catch (CertificateException e) { 282 fail("Unexpected CertificateException " + e.getMessage()); 283 } 284 } 285 286 /** 287 * Test for <code>engineGenerateCertPath(InputStream, String)</code> 288 * method. Assertion: generates a <code>CertPath</code> object and 289 * initializes it with the data read from the <code>InputStream</code> 290 */ 291 public void testEngineGenerateCertPathLjava_io_InputStream_Ljava_lang_String01() { 292 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 293 MyCertificateFactorySpi.putMode(true); 294 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 295 DataInputStream dis = new DataInputStream(bais); 296 try { 297 certFactorySpi.engineGenerateCertPath(dis, ""); 298 fail("IllegalArgumentException expected"); 299 } catch (IllegalArgumentException e) { 300 // expected 301 } catch (CertificateException e) { 302 fail("Unexpected CertificateException " + e.getMessage()); 303 } 304 305 try { 306 assertNull(certFactorySpi.engineGenerateCertPath(dis, "encoding")); 307 } catch (CertificateException e) { 308 fail("Unexpected CertificateException " + e.getMessage()); 309 } 310 } 311 312 /** 313 * Test for <code>engineGenerateCertPath(InputStream, String)</code> 314 * method. Assertion: generates a <code>CertPath</code> object and 315 * initializes it with the data read from the <code>InputStream</code> 316 */ 317 public void testEngineGenerateCertPathLjava_io_InputStream_Ljava_lang_String02() { 318 CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi(); 319 320 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); 321 DataInputStream dis = new DataInputStream(bais); 322 323 try { 324 certFactorySpi.engineGenerateCertPath(dis, "encoding"); 325 fail("UnsupportedOperationException expected"); 326 } catch (UnsupportedOperationException e) { 327 // expected 328 } catch (CertificateException e) { 329 fail("Unexpected CertificateException " + e.getMessage()); 330 } 331 } 332 333 /** 334 * Test for <code>engineGenerateCertPath(List<? extends Certificate>)</code> 335 * method Assertion: generates a <code>CertPath</code> object and 336 * initializes it with a <code>List</code> of <code>Certificates</code> 337 */ 338 public void testEngineGenerateCertPathLJava_util_List01() { 339 CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi(); 340 MyCertificateFactorySpi.putMode(true); 341 List<Certificate> list = new ArrayList<Certificate>(); 342 343 try { 344 assertNull(certFactorySpi.engineGenerateCertPath(list)); 345 } catch (CertificateException e) { 346 fail("Unexpected CertificateException " + e.getMessage()); 347 } 348 349 try { 350 certFactorySpi.engineGenerateCertPath((List<? extends Certificate>)null); 351 fail("expected NullPointerException"); 352 } catch (NullPointerException e) { 353 // ok 354 } catch (CertificateException e) { 355 fail("Unexpected CertificateException " + e.getMessage()); 356 } 357 } 358 359 /** 360 * Test for <code>engineGenerateCertPath(List<? extends Certificate>)</code> 361 * method Assertion: generates a <code>CertPath</code> object and 362 * initializes it with a <code>List</code> of <code>Certificates</code> 363 */ 364 public void testEngineGenerateCertPathLJava_util_List02() { 365 CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi(); 366 367 List<Certificate> list = new ArrayList<Certificate>(); 368 369 try { 370 certFactorySpi.engineGenerateCertPath(list); 371 fail("UnsupportedOperationException expected"); 372 } catch (UnsupportedOperationException e) { 373 // expected 374 } catch (CertificateException e) { 375 fail("Unexpected CertificateException " + e.getMessage()); 376 } 377 } 378 379 public void testAbstractMethods() { 380 CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi(); 381 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[3]); 382 DataInputStream dis = new DataInputStream(bais); 383 384 try { 385 certFactorySpi.engineGenerateCRL(dis); 386 certFactorySpi.engineGenerateCRLs(dis); 387 certFactorySpi.engineGenerateCertificate(dis); 388 certFactorySpi.engineGenerateCertificates(dis); 389 } catch (Exception e) { 390 fail("Unexpected exception " + e.getMessage()); 391 } 392 } 393 394 private static class extCertificateFactorySpi extends CertificateFactorySpi { 395 public Certificate engineGenerateCertificate(InputStream inStream) 396 throws CertificateException { 397 if (inStream == null) { 398 throw new CertificateException("InputStream null"); 399 } 400 return null; 401 } 402 403 @SuppressWarnings("unchecked") 404 public Collection engineGenerateCertificates(InputStream inStream) 405 throws CertificateException { 406 if (inStream == null) { 407 throw new CertificateException("InputStream null"); 408 } 409 return null; 410 } 411 412 public CRL engineGenerateCRL(InputStream inStream) throws CRLException { 413 if (inStream == null) { 414 throw new CRLException("InputStream null"); 415 } 416 return null; 417 } 418 419 @SuppressWarnings("unchecked") 420 public Collection engineGenerateCRLs(InputStream inStream) 421 throws CRLException { 422 if (inStream == null) { 423 throw new CRLException("InputStream null"); 424 } 425 return null; 426 } 427 428 429 430 431 } 432 } 433