DRLCertFactoryTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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.NoSuchProviderException;
26import java.security.cert.CertificateFactory;
27import java.security.cert.CertificateException;
28
29import junit.framework.Test;
30import junit.framework.TestCase;
31import junit.framework.TestSuite;
32
33/**
34 * DRLCertFactoryTest
35 */
36public class DRLCertFactoryTest extends TestCase {
37
38    // The testing data was generated by using of classes
39    // from org.apache.harmony.security.asn1 package.
40
41    // 2 consecutively encoded X.509 certificates
42    private static byte[] certEncoding = new byte[] {
43        (byte) 0x30,(byte) 0x82,(byte) 0x02,(byte) 0xfa,(byte) 0x30,
44        (byte) 0x82,(byte) 0x02,(byte) 0xb6,(byte) 0xa0,(byte) 0x03,
45        (byte) 0x02,(byte) 0x01,(byte) 0x02,(byte) 0x02,(byte) 0x02,
46        (byte) 0x02,(byte) 0x2b,(byte) 0x30,(byte) 0x0c,(byte) 0x06,
47        (byte) 0x07,(byte) 0x2a,(byte) 0x86,(byte) 0x48,(byte) 0xce,
48        (byte) 0x38,(byte) 0x04,(byte) 0x03,(byte) 0x01,(byte) 0x01,
49        (byte) 0x00,(byte) 0x30,(byte) 0x1d,(byte) 0x31,(byte) 0x1b,
50        (byte) 0x30,(byte) 0x19,(byte) 0x06,(byte) 0x03,(byte) 0x55,
51        (byte) 0x04,(byte) 0x0a,(byte) 0x13,(byte) 0x12,(byte) 0x43,
52        (byte) 0x65,(byte) 0x72,(byte) 0x74,(byte) 0x69,(byte) 0x66,
53        (byte) 0x69,(byte) 0x63,(byte) 0x61,(byte) 0x74,(byte) 0x65,
54        (byte) 0x20,(byte) 0x49,(byte) 0x73,(byte) 0x73,(byte) 0x75,
55        (byte) 0x65,(byte) 0x72,(byte) 0x30,(byte) 0x22,(byte) 0x18,
56        (byte) 0x0f,(byte) 0x31,(byte) 0x39,(byte) 0x37,(byte) 0x30,
57        (byte) 0x30,(byte) 0x31,(byte) 0x31,(byte) 0x32,(byte) 0x31,
58        (byte) 0x33,(byte) 0x34,(byte) 0x36,(byte) 0x34,(byte) 0x30,
59        (byte) 0x5a,(byte) 0x18,(byte) 0x0f,(byte) 0x31,(byte) 0x39,
60        (byte) 0x37,(byte) 0x30,(byte) 0x30,(byte) 0x31,(byte) 0x32,
61        (byte) 0x34,(byte) 0x30,(byte) 0x33,(byte) 0x33,(byte) 0x33,
62        (byte) 0x32,(byte) 0x30,(byte) 0x5a,(byte) 0x30,(byte) 0x1f,
63        (byte) 0x31,(byte) 0x1d,(byte) 0x30,(byte) 0x1b,(byte) 0x06,
64        (byte) 0x03,(byte) 0x55,(byte) 0x04,(byte) 0x0a,(byte) 0x13,
65        (byte) 0x14,(byte) 0x53,(byte) 0x75,(byte) 0x62,(byte) 0x6a,
66        (byte) 0x65,(byte) 0x63,(byte) 0x74,(byte) 0x20,(byte) 0x4f,
67        (byte) 0x72,(byte) 0x67,(byte) 0x61,(byte) 0x6e,(byte) 0x69,
68        (byte) 0x7a,(byte) 0x61,(byte) 0x74,(byte) 0x69,(byte) 0x6f,
69        (byte) 0x6e,(byte) 0x30,(byte) 0x19,(byte) 0x30,(byte) 0x0c,
70        (byte) 0x06,(byte) 0x07,(byte) 0x2a,(byte) 0x86,(byte) 0x48,
71        (byte) 0xce,(byte) 0x38,(byte) 0x04,(byte) 0x03,(byte) 0x01,
72        (byte) 0x01,(byte) 0x00,(byte) 0x03,(byte) 0x09,(byte) 0x00,
73        (byte) 0x01,(byte) 0x02,(byte) 0x03,(byte) 0x04,(byte) 0x05,
74        (byte) 0x06,(byte) 0x07,(byte) 0x08,(byte) 0x81,(byte) 0x02,
75        (byte) 0x00,(byte) 0xaa,(byte) 0x82,(byte) 0x02,(byte) 0x00,
76        (byte) 0x55,(byte) 0xa3,(byte) 0x82,(byte) 0x02,(byte) 0x14,
77        (byte) 0x30,(byte) 0x82,(byte) 0x02,(byte) 0x10,(byte) 0x30,
78        (byte) 0x0f,(byte) 0x06,(byte) 0x03,(byte) 0x55,(byte) 0x1d,
79        (byte) 0x0f,(byte) 0x01,(byte) 0x01,(byte) 0xff,(byte) 0x04,
80        (byte) 0x05,(byte) 0x03,(byte) 0x03,(byte) 0x01,(byte) 0xaa,
81        (byte) 0x80,(byte) 0x30,(byte) 0x12,(byte) 0x06,(byte) 0x03,
82        (byte) 0x55,(byte) 0x1d,(byte) 0x13,(byte) 0x01,(byte) 0x01,
83        (byte) 0xff,(byte) 0x04,(byte) 0x08,(byte) 0x30,(byte) 0x06,
84        (byte) 0x01,(byte) 0x01,(byte) 0xff,(byte) 0x02,(byte) 0x01,
85        (byte) 0x05,(byte) 0x30,(byte) 0x14,(byte) 0x06,(byte) 0x03,
86        (byte) 0x55,(byte) 0x1d,(byte) 0x20,(byte) 0x01,(byte) 0x01,
87        (byte) 0xff,(byte) 0x04,(byte) 0x0a,(byte) 0x30,(byte) 0x08,
88        (byte) 0x30,(byte) 0x06,(byte) 0x06,(byte) 0x04,(byte) 0x55,
89        (byte) 0x1d,(byte) 0x20,(byte) 0x00,(byte) 0x30,(byte) 0x67,
90        (byte) 0x06,(byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x11,
91        (byte) 0x01,(byte) 0x01,(byte) 0xff,(byte) 0x04,(byte) 0x5d,
92        (byte) 0x30,(byte) 0x5b,(byte) 0x81,(byte) 0x0c,(byte) 0x72,
93        (byte) 0x66,(byte) 0x63,(byte) 0x40,(byte) 0x38,(byte) 0x32,
94        (byte) 0x32,(byte) 0x2e,(byte) 0x4e,(byte) 0x61,(byte) 0x6d,
95        (byte) 0x65,(byte) 0x82,(byte) 0x07,(byte) 0x64,(byte) 0x4e,
96        (byte) 0x53,(byte) 0x4e,(byte) 0x61,(byte) 0x6d,(byte) 0x65,
97        (byte) 0xa4,(byte) 0x17,(byte) 0x31,(byte) 0x15,(byte) 0x30,
98        (byte) 0x13,(byte) 0x06,(byte) 0x03,(byte) 0x55,(byte) 0x04,
99        (byte) 0x0a,(byte) 0x13,(byte) 0x0c,(byte) 0x4f,(byte) 0x72,
100        (byte) 0x67,(byte) 0x61,(byte) 0x6e,(byte) 0x69,(byte) 0x7a,
101        (byte) 0x61,(byte) 0x74,(byte) 0x69,(byte) 0x6f,(byte) 0x6e,
102        (byte) 0x86,(byte) 0x1a,(byte) 0x68,(byte) 0x74,(byte) 0x74,
103        (byte) 0x70,(byte) 0x3a,(byte) 0x2f,(byte) 0x2f,(byte) 0x75,
104        (byte) 0x6e,(byte) 0x69,(byte) 0x66,(byte) 0x6f,(byte) 0x72,
105        (byte) 0x6d,(byte) 0x2e,(byte) 0x52,(byte) 0x65,(byte) 0x73,
106        (byte) 0x6f,(byte) 0x75,(byte) 0x72,(byte) 0x63,(byte) 0x65,
107        (byte) 0x2e,(byte) 0x49,(byte) 0x64,(byte) 0x87,(byte) 0x04,
108        (byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0x00,(byte) 0x88,
109        (byte) 0x07,(byte) 0x2a,(byte) 0x03,(byte) 0xa2,(byte) 0x5c,
110        (byte) 0x83,(byte) 0xb2,(byte) 0x03,(byte) 0x30,(byte) 0x0c,
111        (byte) 0x06,(byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x1e,
112        (byte) 0x01,(byte) 0x01,(byte) 0xff,(byte) 0x04,(byte) 0x02,
113        (byte) 0x30,(byte) 0x00,(byte) 0x30,(byte) 0x0c,(byte) 0x06,
114        (byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x24,(byte) 0x01,
115        (byte) 0x01,(byte) 0xff,(byte) 0x04,(byte) 0x02,(byte) 0x30,
116        (byte) 0x00,(byte) 0x30,(byte) 0x81,(byte) 0x99,(byte) 0x06,
117        (byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x25,(byte) 0x01,
118        (byte) 0x01,(byte) 0xff,(byte) 0x04,(byte) 0x81,(byte) 0x8e,
119        (byte) 0x30,(byte) 0x81,(byte) 0x8b,(byte) 0x06,(byte) 0x04,
120        (byte) 0x55,(byte) 0x1d,(byte) 0x25,(byte) 0x00,(byte) 0x06,
121        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
122        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x01,(byte) 0x06,
123        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
124        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x01,(byte) 0x06,
125        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
126        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x02,(byte) 0x06,
127        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
128        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x03,(byte) 0x06,
129        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
130        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x04,(byte) 0x06,
131        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
132        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x05,(byte) 0x06,
133        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
134        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x06,(byte) 0x06,
135        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
136        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x07,(byte) 0x06,
137        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
138        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x08,(byte) 0x06,
139        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
140        (byte) 0x05,(byte) 0x07,(byte) 0x03,(byte) 0x09,(byte) 0x06,
141        (byte) 0x08,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x05,
142        (byte) 0x05,(byte) 0x08,(byte) 0x02,(byte) 0x02,(byte) 0x06,
143        (byte) 0x0a,(byte) 0x2b,(byte) 0x06,(byte) 0x01,(byte) 0x04,
144        (byte) 0x01,(byte) 0x82,(byte) 0x37,(byte) 0x0a,(byte) 0x03,
145        (byte) 0x03,(byte) 0x06,(byte) 0x09,(byte) 0x60,(byte) 0x86,
146        (byte) 0x48,(byte) 0x01,(byte) 0x86,(byte) 0xf8,(byte) 0x42,
147        (byte) 0x04,(byte) 0x01,(byte) 0x30,(byte) 0x0d,(byte) 0x06,
148        (byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x36,(byte) 0x01,
149        (byte) 0x01,(byte) 0xff,(byte) 0x04,(byte) 0x03,(byte) 0x02,
150        (byte) 0x01,(byte) 0x01,(byte) 0x30,(byte) 0x0e,(byte) 0x06,
151        (byte) 0x04,(byte) 0x2a,(byte) 0x4d,(byte) 0x86,(byte) 0x09,
152        (byte) 0x01,(byte) 0x01,(byte) 0xff,(byte) 0x04,(byte) 0x03,
153        (byte) 0x01,(byte) 0x01,(byte) 0x01,(byte) 0x30,(byte) 0x64,
154        (byte) 0x06,(byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x12,
155        (byte) 0x04,(byte) 0x5d,(byte) 0x30,(byte) 0x5b,(byte) 0x81,
156        (byte) 0x0c,(byte) 0x72,(byte) 0x66,(byte) 0x63,(byte) 0x40,
157        (byte) 0x38,(byte) 0x32,(byte) 0x32,(byte) 0x2e,(byte) 0x4e,
158        (byte) 0x61,(byte) 0x6d,(byte) 0x65,(byte) 0x82,(byte) 0x07,
159        (byte) 0x64,(byte) 0x4e,(byte) 0x53,(byte) 0x4e,(byte) 0x61,
160        (byte) 0x6d,(byte) 0x65,(byte) 0xa4,(byte) 0x17,(byte) 0x31,
161        (byte) 0x15,(byte) 0x30,(byte) 0x13,(byte) 0x06,(byte) 0x03,
162        (byte) 0x55,(byte) 0x04,(byte) 0x0a,(byte) 0x13,(byte) 0x0c,
163        (byte) 0x4f,(byte) 0x72,(byte) 0x67,(byte) 0x61,(byte) 0x6e,
164        (byte) 0x69,(byte) 0x7a,(byte) 0x61,(byte) 0x74,(byte) 0x69,
165        (byte) 0x6f,(byte) 0x6e,(byte) 0x86,(byte) 0x1a,(byte) 0x68,
166        (byte) 0x74,(byte) 0x74,(byte) 0x70,(byte) 0x3a,(byte) 0x2f,
167        (byte) 0x2f,(byte) 0x75,(byte) 0x6e,(byte) 0x69,(byte) 0x66,
168        (byte) 0x6f,(byte) 0x72,(byte) 0x6d,(byte) 0x2e,(byte) 0x52,
169        (byte) 0x65,(byte) 0x73,(byte) 0x6f,(byte) 0x75,(byte) 0x72,
170        (byte) 0x63,(byte) 0x65,(byte) 0x2e,(byte) 0x49,(byte) 0x64,
171        (byte) 0x87,(byte) 0x04,(byte) 0xff,(byte) 0xff,(byte) 0xff,
172        (byte) 0x00,(byte) 0x88,(byte) 0x07,(byte) 0x2a,(byte) 0x03,
173        (byte) 0xa2,(byte) 0x5c,(byte) 0x83,(byte) 0xb2,(byte) 0x03,
174        (byte) 0x30,(byte) 0x09,(byte) 0x06,(byte) 0x03,(byte) 0x55,
175        (byte) 0x1d,(byte) 0x1f,(byte) 0x04,(byte) 0x02,(byte) 0x30,
176        (byte) 0x00,(byte) 0x30,(byte) 0x0a,(byte) 0x06,(byte) 0x03,
177        (byte) 0x55,(byte) 0x1d,(byte) 0x23,(byte) 0x04,(byte) 0x03,
178        (byte) 0x01,(byte) 0x01,(byte) 0x01,(byte) 0x30,(byte) 0x0a,
179        (byte) 0x06,(byte) 0x03,(byte) 0x55,(byte) 0x1d,(byte) 0x0e,
180        (byte) 0x04,(byte) 0x03,(byte) 0x01,(byte) 0x01,(byte) 0x01,
181        (byte) 0x30,(byte) 0x0a,(byte) 0x06,(byte) 0x03,(byte) 0x55,
182        (byte) 0x1d,(byte) 0x21,(byte) 0x04,(byte) 0x03,(byte) 0x01,
183        (byte) 0x01,(byte) 0x01,(byte) 0x30,(byte) 0x0c,(byte) 0x06,
184        (byte) 0x07,(byte) 0x2a,(byte) 0x86,(byte) 0x48,(byte) 0xce,
185        (byte) 0x38,(byte) 0x04,(byte) 0x03,(byte) 0x01,(byte) 0x01,
186        (byte) 0x00,(byte) 0x03,(byte) 0x30,(byte) 0x00,(byte) 0x30,
187        (byte) 0x2d,(byte) 0x02,(byte) 0x14,(byte) 0x00,(byte) 0xbe,
188        (byte) 0x10,(byte) 0xbe,(byte) 0x86,(byte) 0xb3,(byte) 0x35,
189        (byte) 0x63,(byte) 0xfb,(byte) 0x8e,(byte) 0xb8,(byte) 0xfc,
190        (byte) 0xe3,(byte) 0xf1,(byte) 0x99,(byte) 0x96,(byte) 0x19,
191        (byte) 0xb4,(byte) 0xf4,(byte) 0x3b,(byte) 0x02,(byte) 0x15,
192        (byte) 0x00,(byte) 0x81,(byte) 0x28,(byte) 0x62,(byte) 0x82,
193        (byte) 0x19,(byte) 0xba,(byte) 0xcf,(byte) 0xeb,(byte) 0x85,
194        (byte) 0x6d,(byte) 0x52,(byte) 0xde,(byte) 0x09,(byte) 0x3e,
195        (byte) 0x62,(byte) 0xe3,(byte) 0x5b,(byte) 0x55,(byte) 0x71,
196        (byte) 0x40,
197    };
198
199    /**
200     * DRLCertFactory() method testing.
201     */
202    public void testDRLCertFactory() {
203        CertificateFactory certFactory;
204        try {
205            certFactory =
206                CertificateFactory.getInstance("X509", "DRLCertFactory");
207        } catch (CertificateException e) {
208            fail("X509 certificate type is not available: "+e.getMessage());
209            return;
210        } catch (NoSuchProviderException e) {
211            fail("Unexpected NoSuchProviderException: "+e.getMessage());
212            return;
213        }
214        // DER encoded certificate generation testing
215        ByteArrayInputStream bais = new ByteArrayInputStream(certEncoding);
216        try {
217            assertNotNull("Generated certificate is null",
218                    certFactory.generateCertificate(bais));
219        } catch (CertificateException e) {
220            fail("Unexpected CertificateException: " + e.getMessage());
221        }
222
223        try {
224            certFactory.generateCertificate(bais);
225            fail("Expected CertificateException was not thrown.");
226        } catch (CertificateException e) {
227        }
228    }
229
230    public static Test suite() {
231        return new TestSuite(DRLCertFactoryTest.class);
232    }
233
234    public static void main(String[] args) {
235        junit.textui.TestRunner.run(suite());
236    }
237}
238
239