001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.provider.cert;
022:
023: import java.io.ByteArrayInputStream;
024: import java.security.cert.CertPath;
025: import java.security.cert.CertificateEncodingException;
026: import java.security.cert.CertificateException;
027: import java.security.cert.CertificateFactory;
028: import java.security.cert.X509Certificate;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036:
037: import org.apache.harmony.security.provider.cert.X509CertPathImpl;
038:
039: /**
040: * X509CertPathImplTest
041: */
042: public class X509CertPathImplTest extends TestCase {
043:
044: private X509Certificate certificate;
045: {
046: try {
047: X509CertImplTest test = new X509CertImplTest();
048: test.setUp();
049: certificate = test.certificate;
050: } catch (Exception e) {
051: e.printStackTrace();
052: }
053: }
054: private X509CertPathImpl certPath;
055: private List certList;
056:
057: protected void setUp() throws java.lang.Exception {
058: certList = new ArrayList();
059: for (int i = 0; i < 2; i++) {
060: certList.add(certificate);
061: }
062: certPath = new X509CertPathImpl(certList);
063: }
064:
065: /**
066: * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.X509CertPathImpl(List)
067: */
068: public void test_X509CertPathImpl_List() throws Exception {
069: assertEquals("Certificate list size missmatch",
070: certList.size(), certPath.getCertificates().size());
071: }
072:
073: /**
074: * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getInstance(InputStream)
075: */
076: public void test_getInstance_InputStream() throws Exception {
077: byte[] encoding = certPath.getEncoded();
078: ByteArrayInputStream bais = new ByteArrayInputStream(encoding);
079: X509CertPathImpl cpath = X509CertPathImpl.getInstance(bais);
080: assertEquals("Certificate list size missmatch",
081: certList.size(), cpath.getCertificates().size());
082: }
083:
084: /**
085: * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getInstance(byte[])
086: */
087: public void test_getInstance_$B() throws Exception {
088: byte[] encoding = certPath.getEncoded();
089: X509CertPathImpl cpath = X509CertPathImpl.getInstance(encoding);
090: assertEquals("Certificate list size missmatch",
091: certList.size(), cpath.getCertificates().size());
092: }
093:
094: /**
095: * @tests org.apache.harmony.security.provider.cert.X509CertPathImpl.getInstance(byte[], String)
096: */
097: public void test_getInstance$BLjava_lang_String() throws Exception {
098:
099: // Test: getInstance(byte[] in, "PKCS7")
100: // reconverting of the encoded form: from default (PkiPath) to PKCS7
101: byte[] encoding = certPath.getEncoded();
102:
103: CertificateFactory factory = CertificateFactory
104: .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.getInstance(encoding,
113: "PKCS7");
114: assertEquals("Certificate list size missmatch",
115: certList.size(), cpath.getCertificates().size());
116:
117: bais = new ByteArrayInputStream(encoding);
118:
119: cpath = X509CertPathImpl.getInstance(bais, "PKCS7");
120: assertEquals("Certificate list size missmatch",
121: certList.size(), cpath.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
131: .getInstance(encoding);
132: assertEquals("Certificate list size missmatch", certList
133: .size(), cpath.getCertificates().size());
134: cpath.getCertificates().remove(0);
135: fail("UnsupportedOperationException should be thrown");
136: } catch (UnsupportedOperationException e) {
137: //pass
138: }
139: }
140:
141: /**
142: * getEncoded() method testing.
143: */
144: public void testGetEncoded1() throws Exception {
145: certPath.getEncoded();
146: }
147:
148: /**
149: * getEncoded(String encoding) method testing.
150: */
151: public void testGetEncoded2() {
152: try {
153: certPath.getEncoded("ABRACADABRA");
154: fail("CertificateEncodingException should be thrown");
155: } catch (CertificateEncodingException e) {
156: }
157: }
158:
159: /**
160: * getEncodings() method testing.
161: */
162: public void testGetEncodings() {
163: try {
164: Iterator it = certPath.getEncodings();
165: Object encoding = it.next();
166: assertNotNull("Default encodings should not be null",
167: encoding);
168: it.remove();
169: fail("UnsupportedOperationException should be thrown");
170: } catch (UnsupportedOperationException e) {
171: // pass
172: }
173: }
174:
175: public static Test suite() {
176: return new TestSuite(X509CertPathImplTest.class);
177: }
178:
179: public static void main(String[] args) {
180: junit.textui.TestRunner.run(suite());
181: }
182: }
|