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 java.security.cert;
022:
023: import java.io.ByteArrayInputStream;
024: import java.math.BigInteger;
025: import java.security.InvalidKeyException;
026: import java.security.NoSuchAlgorithmException;
027: import java.security.NoSuchProviderException;
028: import java.security.Principal;
029: import java.security.PublicKey;
030: import java.security.SignatureException;
031: import java.util.Date;
032: import java.util.Set;
033:
034: import javax.security.auth.x500.X500Principal;
035:
036: import org.apache.harmony.luni.util.Base64;
037: import org.apache.harmony.security.tests.support.cert.TestUtils;
038:
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: /**
044: * X509CertificateTest
045: */
046: public class X509CertificateTest extends TestCase {
047:
048: // Base64 encoded form of ASN.1 DER encoded X.509 Certificate
049: // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt)
050: // (generated by using of classes from
051: // org.apache.harmony.security.x509 package)
052: static String base64cert = "MIIByzCCATagAwIBAgICAiswCwYJKoZIhvcNAQEFMB0xGzAZBgNVBAoT"
053: + "EkNlcnRpZmljYXRlIElzc3VlcjAeFw0wNjA0MjYwNjI4MjJaFw0zMzAz"
054: + "MDExNjQ0MDlaMB0xGzAZBgNVBAoTEkNlcnRpZmljYXRlIElzc3VlcjCB"
055: + "nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAkLGLsPdSPDMyP1OUOKu+"
056: + "U3cvbNK5RGaQ3bXc5aDjvApx43BcaoXgt6YD/5yXz0OsIooj5yA37+bY"
057: + "JGcVrvFD5FMPdDd3vjNPQOep0MzG4CdbkaZde5SigPabOMQYS4oUyLBx"
058: + "W3LGG0mUODe5AGGqtqXU0GlKg4K2je6cCtookCUCAwEAAaMeMBwwGgYD"
059: + "VR0RAQH/BBAwDoEMcmZjQDgyMi5OYW1lMAsGCSqGSIb3DQEBBQOBgQBZ"
060: + "pVXj01dOpqnZErU+Qb50j8lJD1dIaz1eJTvJCSadj7ziV1VtnnapI07c"
061: + "XEa7ONzcHQTYTG10poHfOK/a0BaULF3GlctDESilwQYbW5BdfpAlZpbH"
062: + "AFLcUDh6Eq50kc+0A/anh/j3mgBNuvbIMo7hHNnZB6k/prswm2BszyLD"
063: + "yw==";
064:
065: // Base64 encoded form of ASN.1 DER encoded X.509 CRL
066: // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt)
067: // (generated by using of classes from
068: // org.apache.harmony.security.x509 package)
069: static String base64crl = "MIHXMIGXAgEBMAkGByqGSM44BAMwFTETMBEGA1UEChMKQ1JMIElzc3Vl"
070: + "chcNMDYwNDI3MDYxMzQ1WhcNMDYwNDI3MDYxNTI1WjBBMD8CAgIrFw0w"
071: + "NjA0MjcwNjEzNDZaMCowCgYDVR0VBAMKAQEwHAYDVR0YBBUYEzIwMDYw"
072: + "NDI3MDYxMzQ1LjQ2OFqgDzANMAsGA1UdFAQEBAQEBDAJBgcqhkjOOAQD"
073: + "AzAAMC0CFQCk0t0DTyu82QpajbBlxX9uXvUDSgIUSBN4g+xTEeexs/0k"
074: + "9AkjBhjF0Es=";
075:
076: // has stub implementation for abstract methods
077: private static class MyX509Certificate extends X509Certificate {
078:
079: public void checkValidity() throws CertificateExpiredException,
080: CertificateNotYetValidException {
081: }
082:
083: public void checkValidity(Date date)
084: throws CertificateExpiredException,
085: CertificateNotYetValidException {
086: }
087:
088: public int getVersion() {
089: return 3;
090: }
091:
092: public BigInteger getSerialNumber() {
093: return null;
094: }
095:
096: public Principal getIssuerDN() {
097: return null;
098: }
099:
100: public Principal getSubjectDN() {
101: return null;
102: }
103:
104: public Date getNotBefore() {
105: return null;
106: }
107:
108: public Date getNotAfter() {
109: return null;
110: }
111:
112: public byte[] getTBSCertificate()
113: throws CertificateEncodingException {
114: return null;
115: }
116:
117: public byte[] getSignature() {
118: return null;
119: }
120:
121: public String getSigAlgName() {
122: return null;
123: }
124:
125: public String getSigAlgOID() {
126: return null;
127: }
128:
129: public byte[] getSigAlgParams() {
130: return null;
131: }
132:
133: public boolean[] getIssuerUniqueID() {
134: return null;
135: }
136:
137: public boolean[] getSubjectUniqueID() {
138: return null;
139: }
140:
141: public boolean[] getKeyUsage() {
142: return null;
143: }
144:
145: public int getBasicConstraints() {
146: return 0;
147: }
148:
149: public void verify(PublicKey key) throws CertificateException,
150: NoSuchAlgorithmException, InvalidKeyException,
151: NoSuchProviderException, SignatureException {
152: }
153:
154: public void verify(PublicKey key, String sigProvider)
155: throws CertificateException, NoSuchAlgorithmException,
156: InvalidKeyException, NoSuchProviderException,
157: SignatureException {
158: }
159:
160: public String toString() {
161: return "";
162: }
163:
164: public PublicKey getPublicKey() {
165: return null;
166: }
167:
168: public byte[] getEncoded() throws CertificateEncodingException {
169: return null;
170: }
171:
172: public Set getNonCriticalExtensionOIDs() {
173: return null;
174: }
175:
176: public Set getCriticalExtensionOIDs() {
177: return null;
178: }
179:
180: public byte[] getExtensionValue(String oid) {
181: return null;
182: }
183:
184: public boolean hasUnsupportedCriticalExtension() {
185: return false;
186: }
187: }
188:
189: /**
190: * @tests java.security.cert.X509Certificate#getType()
191: */
192: public void testGetType() {
193: assertEquals("X.509", new MyX509Certificate().getType());
194: }
195:
196: /**
197: * @tests java.security.cert.X509Certificate#getIssuerX500Principal()
198: */
199: public void testGetIssuerX500Principal() {
200: // return valid encoding
201: MyX509Certificate cert = new MyX509Certificate() {
202: public byte[] getEncoded() {
203: return TestUtils.getX509Certificate_v1();
204: };
205: };
206:
207: assertEquals(new X500Principal("CN=Z"), cert
208: .getIssuerX500Principal());
209: }
210:
211: /**
212: * @tests java.security.cert.X509Certificate#getSubjectX500Principal()
213: */
214: public void testGetSubjectX500Principal() {
215: // return valid encoding
216: MyX509Certificate cert = new MyX509Certificate() {
217: public byte[] getEncoded() {
218: return TestUtils.getX509Certificate_v1();
219: };
220: };
221:
222: assertEquals(new X500Principal("CN=Y"), cert
223: .getSubjectX500Principal());
224: }
225:
226: /**
227: * @tests java.security.cert.X509Certificate#getExtendedKeyUsage()
228: */
229: public void testGetExtendedKeyUsage()
230: throws CertificateParsingException {
231: assertNull(new MyX509Certificate().getExtendedKeyUsage());
232: }
233:
234: /**
235: * @tests java.security.cert.X509Certificate#getSubjectAlternativeNames()
236: */
237: public void testGetSubjectAlternativeNames()
238: throws CertificateParsingException {
239:
240: assertNull(new MyX509Certificate().getSubjectAlternativeNames());
241: }
242:
243: /**
244: * @tests java.security.cert.X509Certificate#getIssuerAlternativeNames()
245: */
246: public void testGetIssuerAlternativeNames()
247: throws CertificateParsingException {
248:
249: assertNull(new MyX509Certificate().getIssuerAlternativeNames());
250: }
251:
252: /**
253: * @tests java.security.cert.X509Certificate#getExtensionValue()
254: */
255: public void testGetExtensionValue() throws Exception {
256: // Regression for HARMONY-419
257: ByteArrayInputStream is = null;
258: CertificateFactory certFactory = CertificateFactory
259: .getInstance("X.509");
260: is = new ByteArrayInputStream(Base64.decode(base64cert
261: .getBytes()));
262: X509Certificate cert = (X509Certificate) certFactory
263: .generateCertificate(is);
264: cert.getExtensionValue("1.1.1.1");
265:
266: is = new ByteArrayInputStream(Base64.decode(base64crl
267: .getBytes()));
268: X509CRL crl = (X509CRL) certFactory.generateCRL(is);
269: crl.getExtensionValue("1.1.1.1");
270: }
271:
272: public static Test suite() {
273: return new TestSuite(X509CertificateTest.class);
274: }
275:
276: public static void main(String[] args) {
277: junit.textui.TestRunner.run(suite());
278: }
279: }
|