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 Alexander Y. Kleymenov 20*/ 21 22package org.apache.harmony.security.tests.provider.cert; 23 24import java.io.ByteArrayInputStream; 25import java.security.cert.CertPath; 26import java.security.cert.CertificateEncodingException; 27import java.security.cert.CertificateException; 28import java.security.cert.CertificateFactory; 29import java.security.cert.X509Certificate; 30import java.util.ArrayList; 31import java.util.Iterator; 32import java.util.List; 33 34import junit.framework.Test; 35import junit.framework.TestCase; 36import junit.framework.TestSuite; 37 38import org.apache.harmony.security.provider.cert.X509CertPathImpl; 39 40/** 41 * X509CertPathImplTest 42 */ 43public class X509CertPathImplTest extends TestCase { 44 45 private X509Certificate certificate; 46 { 47 try { 48 X509CertImplTest test = new X509CertImplTest(); 49 test.setUp(); 50 certificate = test.certificate; 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 } 55 private X509CertPathImpl certPath; 56 private List certList; 57 58 protected void setUp() throws java.lang.Exception { 59 certList = new ArrayList(); 60 for (int i=0; i<2; i++) { 61 certList.add(certificate); 62 } 63 certPath = new X509CertPathImpl(certList); 64 } 65 66 /** 67 * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.X509CertPathImpl(List) 68 */ 69 public void test_X509CertPathImpl_List() throws Exception { 70 assertEquals("Certificate list size missmatch", 71 certList.size(), certPath.getCertificates().size()); 72 } 73 74 /** 75 * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getInstance(InputStream) 76 */ 77 public void test_getInstance_InputStream() throws Exception { 78 byte[] encoding = certPath.getEncoded(); 79 ByteArrayInputStream bais = new ByteArrayInputStream(encoding); 80 X509CertPathImpl cpath = X509CertPathImpl.getInstance(bais); 81 assertEquals("Certificate list size missmatch", certList.size(), cpath 82 .getCertificates().size()); 83 } 84 85 /** 86 * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getInstance(byte[]) 87 */ 88 public void test_getInstance_$B() throws Exception { 89 byte[] encoding = certPath.getEncoded(); 90 X509CertPathImpl cpath = X509CertPathImpl.getInstance(encoding); 91 assertEquals("Certificate list size missmatch", certList.size(), cpath 92 .getCertificates().size()); 93 } 94 95 /** 96 * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getInstance(byte[], String) 97 */ 98 public void test_getInstance$BLjava_lang_String() throws Exception { 99 100 // Test: getInstance(byte[] in, "PKCS7") 101 // reconverting of the encoded form: from default (PkiPath) to PKCS7 102 byte[] encoding = certPath.getEncoded(); 103 104 CertificateFactory factory = CertificateFactory.getInstance("X.509"); 105 106 ByteArrayInputStream bais = new ByteArrayInputStream(encoding); 107 108 CertPath cert_path = factory.generateCertPath(bais); 109 110 encoding = cert_path.getEncoded("PKCS7"); 111 112 X509CertPathImpl cpath = X509CertPathImpl 113 .getInstance(encoding, "PKCS7"); 114 assertEquals("Certificate list size missmatch", certList.size(), cpath 115 .getCertificates().size()); 116 117 bais = new ByteArrayInputStream(encoding); 118 119 cpath = X509CertPathImpl.getInstance(bais, "PKCS7"); 120 assertEquals("Certificate list size missmatch", certList.size(), cpath 121 .getCertificates().size()); 122 } 123 124 /** 125 * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getCertificates() 126 */ 127 public void test_getCertificates() throws Exception { 128 try { 129 byte[] encoding = certPath.getEncoded(); 130 X509CertPathImpl cpath = X509CertPathImpl.getInstance(encoding); 131 assertEquals("Certificate list size missmatch", certList.size(), 132 cpath.getCertificates().size()); 133 cpath.getCertificates().remove(0); 134 fail("UnsupportedOperationException should be thrown"); 135 } catch (UnsupportedOperationException e) { 136 //pass 137 } 138 } 139 140 /** 141 * getEncoded() method testing. 142 */ 143 public void testGetEncoded1() throws Exception { 144 certPath.getEncoded(); 145 } 146 147 /** 148 * getEncoded(String encoding) method testing. 149 */ 150 public void testGetEncoded2() { 151 try { 152 certPath.getEncoded("ABRACADABRA"); 153 fail("CertificateEncodingException should be thrown"); 154 } catch (CertificateEncodingException e) { 155 } 156 } 157 158 /** 159 * getEncodings() method testing. 160 */ 161 public void testGetEncodings() { 162 try { 163 Iterator it = certPath.getEncodings(); 164 Object encoding = it.next(); 165 assertNotNull("Default encodings should not be null", encoding); 166 it.remove(); 167 fail("UnsupportedOperationException should be thrown"); 168 } catch (UnsupportedOperationException e) { 169 // pass 170 } 171 } 172 173 public static Test suite() { 174 return new TestSuite(X509CertPathImplTest.class); 175 } 176 177} 178