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 org.apache.harmony.security.tests.support.cert; 24 25 import java.io.DataInputStream; 26 import java.io.InputStream; 27 import java.security.cert.CRL; 28 import java.security.cert.CRLException; 29 import java.security.cert.CertPath; 30 import java.security.cert.Certificate; 31 import java.security.cert.CertificateException; 32 import java.security.cert.CertificateFactorySpi; 33 import java.util.Collection; 34 import java.util.HashSet; 35 import java.util.Iterator; 36 import java.util.List; 37 import java.util.Set; 38 39 /** 40 * Additional class for verification CertificateFactorySpi and 41 * CertificateFactory classes 42 * 43 */ 44 45 public class MyCertificateFactorySpi extends CertificateFactorySpi { 46 // Variants of execution: 47 // mode: false - list of encodings is empty 48 // mode: true - list of encodings consists of 2 elements 49 // some exceptions are thrown when 50 private static boolean mode; 51 52 private Set<String> list; 53 MyCertificateFactorySpi()54 public MyCertificateFactorySpi() { 55 super(); 56 mode = true; 57 list = new HashSet<String>(); 58 list.add("aa"); 59 list.add("bb"); 60 } 61 putMode(boolean newMode)62 public static void putMode(boolean newMode) { 63 mode = newMode; 64 } 65 engineGenerateCertificate(InputStream inStream)66 public Certificate engineGenerateCertificate(InputStream inStream) 67 throws CertificateException { 68 if (!(inStream instanceof DataInputStream)) { 69 throw new CertificateException("Incorrect inputstream"); 70 } 71 return null; 72 } 73 74 @SuppressWarnings("unchecked") engineGenerateCertificates(InputStream inStream)75 public Collection engineGenerateCertificates(InputStream inStream) 76 throws CertificateException { 77 if (!(inStream instanceof DataInputStream)) { 78 throw new CertificateException("Incorrect inputstream"); 79 } 80 return null; 81 } 82 engineGenerateCRL(InputStream inStream)83 public CRL engineGenerateCRL(InputStream inStream) throws CRLException { 84 if (!(inStream instanceof DataInputStream)) { 85 throw new CRLException("Incorrect inputstream"); 86 } 87 return null; 88 } 89 90 @SuppressWarnings("unchecked") engineGenerateCRLs(InputStream inStream)91 public Collection engineGenerateCRLs(InputStream inStream) 92 throws CRLException { 93 if (!(inStream instanceof DataInputStream)) { 94 throw new CRLException("Incorrect inputstream"); 95 } 96 return null; 97 } 98 engineGenerateCertPath(InputStream inStream)99 public CertPath engineGenerateCertPath(InputStream inStream) 100 throws CertificateException { 101 if (!(inStream instanceof DataInputStream)) { 102 throw new CertificateException("Incorrect inputstream"); 103 } 104 Iterator<String> it = engineGetCertPathEncodings(); 105 if (!it.hasNext()) { 106 throw new CertificateException("There are no CertPath encodings"); 107 } 108 return engineGenerateCertPath(inStream, it.next()); 109 } 110 engineGenerateCertPath(InputStream inStream, String encoding)111 public CertPath engineGenerateCertPath(InputStream inStream, String encoding) 112 throws CertificateException { 113 if (!(inStream instanceof DataInputStream)) { 114 throw new CertificateException("Incorrect inputstream"); 115 } 116 if (encoding.length() == 0) { 117 if (mode) { 118 throw new IllegalArgumentException("Encoding is empty"); 119 } 120 } 121 return null; 122 } 123 engineGenerateCertPath(List<? extends Certificate> certificates)124 public CertPath engineGenerateCertPath(List<? extends Certificate> certificates) { 125 if (certificates == null) { 126 if (mode) { 127 throw new NullPointerException("certificates is null"); 128 } 129 } 130 return null; 131 } 132 engineGetCertPathEncodings()133 public Iterator<String> engineGetCertPathEncodings() { 134 if (!mode) { 135 list.clear(); 136 } 137 return list.iterator(); 138 } 139 }